Select text in a drawing and this VBA macro writes each text's position, height, layer and content to a CSV file for Excel. Use it to pull room names, levels, chainages or labels out of a drawing.
ExportsX, Y, Z, Height, Layer, Text
Works onTEXT and MTEXT
Outputtext.csv, next to the drawing
The macro
Sub ExportText()
Dim ss As AcadSelectionSet, obj As AcadEntity, c As Variant
Dim f As String, t 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 & "\text.csv"
Open f For Output As #1
Print #1, "X,Y,Z,Height,Layer,Text"
For Each obj In ss
If TypeOf obj Is AcadText Or TypeOf obj Is AcadMText Then
c = obj.InsertionPoint
t = Replace(obj.TextString, """", """""")
Print #1, Trim(Str(c(0))) & "," & Trim(Str(c(1))) & "," & Trim(Str(c(2))) & "," & Trim(Str(obj.Height)) & "," & obj.Layer & ",""" & t & """"
End If
Next obj
Close #1
ss.Delete
MsgBox "Saved to " & f, , "XL n CAD"
End Sub
Select the text (or a window around the whole drawing) and press Enter. The text is wrapped in quotes, so commas inside it don't split the CSV columns.
How to run the macro
- AutoCAD 2010 and later need the free VBA Enabler 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. - Type
VBARUN, pick ExportText and click Run. - Open text.csv in Excel.
What changed from the original code
| Original code | Problem | Fixed |
|---|---|---|
| Curly quotes “ ” | The code won't run | Straight quotes |
Only AcDbText |
Multiline text (MTEXT) was skipped | TEXT and MTEXT |
Fixed E: path |
Fails on most computers | Saves next to the drawing |
| Text not quoted | A comma in the text split it across columns | Quoted, CSV-safe |
| Selection set added before deleting the old one | Fails on the second run | Old set deleted first |
Things to know
- MTEXT content includes formatting codes such as
\P(new paragraph). Clean them in Excel with=SUBSTITUTE(F2,"\P"," "). - Text inside blocks or dimensions isn't selected; explode a copy of the block first if you need it.
- To put text into AutoCAD from Excel, see import text from Excel to AutoCAD.