AutoCAD Macro to Export Polyline Areas to Excel (page)

This VBA macro writes the area, perimeter, vertex count and layer of every polyline and polygon in the drawing to a CSV file. Ideal for plot areas, room areas and quantity take-offs.

ExportsArea, Perimeter, Vertices, Closed, Layer, start point
Works onLWPOLYLINE, including POLYGON shapes
Outputpolylines.csv, next to the drawing

The macro

Sub ExportPolylineAreas()
    Dim obj As AcadEntity, c As Variant, f As String
    f = ThisDrawing.Path & "\polylines.csv"
    Open f For Output As #1
    Print #1, "Area,Perimeter,Vertices,Closed,Layer,StartX,StartY"
    For Each obj In ThisDrawing.ModelSpace
        If TypeOf obj Is AcadLWPolyline Then
            c = obj.Coordinates
            Print #1, Trim(Str(obj.Area)) & "," & Trim(Str(obj.Length)) & "," & (UBound(c) + 1) / 2 & "," & obj.Closed & "," & obj.Layer & "," & Trim(Str(c(0))) & "," & Trim(Str(c(1)))
        End If
    Next obj
    Close #1
    MsgBox "Saved to " & f, , "XL n CAD"
End Sub

Shapes drawn with POLYGON and RECTANG are lightweight polylines too, so they're included.

How to run the macro

  1. AutoCAD 2010 and later need the free VBA Enabler from Autodesk for your version. AutoCAD LT doesn't support VBA.
  2. Save the drawing first: the file is written to the drawing's folder.
  3. Type VBAIDE (or press Alt + F11), choose Insert → Module, and paste the code.
  4. Type VBARUN, pick ExportPolylineAreas and click Run.
  5. Open polylines.csv in Excel.

What changed from the original code

Original code Problem Fixed
Curly quotes “ ” The code won't run Straight quotes
Fixed E: path Fails on most computers Saves next to the drawing
Area only No way to tell open from closed shapes Adds Closed, perimeter and vertex count
Two text lines per polyline Hard to use in Excel One CSV row each, with headers

Things to know

  • For an open polyline, AutoCAD reports the area as if it were closed with a straight line. Filter on Closed = True for real areas.
  • Area is in drawing units squared: divide by 1,000,000 for m² if the drawing is in mm, or by 10,000 for hectares if it's in metres.
  • Hatches, regions and circles aren't polylines. For circles, use the circle area macro.
  • For each vertex's coordinates, use the 2D polyline coordinates macro.

Related