Select blocks on screen and this VBA macro writes each block’s name, insertion point and layer to a CSV file. Use it to count and locate doors, lamps, manholes or any symbol in a drawing.
ExportsBlock name, X, Y, Z, Layer
SelectionPick on screen; other objects are ignored
Outputblocks.csv, next to the drawing
The macro
Sub ExportBlocks()
Dim ss As AcadSelectionSet, obj As AcadEntity, c As Variant
Dim f As String
On Error Resume Next
ThisDrawing.SelectionSets("XLnCAD").Delete
On Error GoTo 0
Set ss = ThisDrawing.SelectionSets.Add("XLnCAD")
ss.SelectOnScreen
f = ThisDrawing.Path & "\blocks.csv"
Open f For Output As #1
Print #1, "Block,X,Y,Z,Layer"
For Each obj In ss
If TypeOf obj Is AcadBlockReference Then
c = obj.InsertionPoint
Print #1, obj.EffectiveName & "," & Trim(Str(c(0))) & "," & Trim(Str(c(1))) & "," & Trim(Str(c(2))) & "," & obj.Layer
End If
Next obj
Close #1
ss.Delete
MsgBox "Saved to " & f, , "XL n CAD"
End Sub
Select the blocks (or a window around the whole drawing) and press Enter.
How to run the macro
- AutoCAD 2010 and later need the free VBA Enabler (VBA module) from Autodesk for your version. AutoCAD LT doesn’t support VBA.
- Save the drawing first: the file is written to the drawing’s folder.
- Type
VBAIDE(or press Alt + F11), choose Insert → Module, and paste the code. - Close the editor, type
VBARUN(or Alt + F8), pick the macro and click Run. - Open the CSV file in Excel.
Numbers are written with Str(), which always uses a dot as the decimal separator, so the CSV opens correctly in Excel whatever your Windows region settings.
What changed from the original code
| Original code | Problem | Fixed |
|---|---|---|
| Curly quotes “ ” | The code won’t run | Straight quotes |
XLnCADObject.Name&; |
Syntax error | Joined with commas |
.Name |
Dynamic blocks show as U123 instead of their real name | .EffectiveName |
| Selection set added before deleting the old one | Fails on the second run | Old set deleted first |
Fixed E: path |
Fails on most computers | Saves next to the drawing |
Count blocks in Excel
Open blocks.csv, select the data and insert a PivotTable with Block in Rows and Values. You get a count of each block type, a quick schedule of quantities. See count each item in a list.