This VBA macro writes the X, Y and Z coordinates and layer of every point in the drawing to a CSV file you can open in Excel. Useful for survey points, levels and setting-out data.
ExportsX, Y, Z, Layer of every POINT
Outputpoints.csv, next to the drawing
NeedsAutoCAD (not LT) with VBA Enabler
The macro
Sub ExportPoints()
Dim obj As AcadEntity, c As Variant
Dim f As String
f = ThisDrawing.Path & "\points.csv"
Open f For Output As #1
Print #1, "X,Y,Z,Layer"
For Each obj In ThisDrawing.ModelSpace
If TypeOf obj Is AcadPoint Then
c = obj.Coordinates
Print #1, Trim(Str(c(0))) & "," & Trim(Str(c(1))) & "," & Trim(Str(c(2))) & "," & obj.Layer
End If
Next obj
Close #1
MsgBox "Saved to " & f, , "XL n CAD"
End Sub
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 “ ” | VBA can’t read them, so the code won’t run | Straight quotes |
"E:ExtractedDetails.Txt" |
Missing backslash, and a fixed drive most people don’t have | Saves next to the drawing |
Dim X, Y, Z As Double |
Only Z is a Double; X and Y are Variants | Reads the coordinate array directly |
| Space-separated .txt | Has to be split into columns in Excel | CSV with a header row |
Variations
- Only points on one layer: change the
Ifline toIf TypeOf obj Is AcadPoint And obj.Layer = "SURVEY" Then. - Round to 3 decimals: use
Trim(Str(Round(c(0), 3))). - Want to plot the points back in AutoCAD later? See import X, Y, Z coordinates from Excel to AutoCAD.