Code Red > VB(A)

Excel VBA to Select polyline and bring area back to excel

(1/4) > >>

MSTG007:
Is there an excel vba that if I push a button, that it will go to the autocad session and I would pick a polyline(s) then report the values back to the excel spreadsheet where I started? (Area, z, etc.)?

RICVBA:
Option Explicit


--- Code: ---Sub PickLwPolyAndGetData()
   
    Dim MyCell As Range
    Dim ACAD As AcadApplication
    Dim LWPoly As AcadLWPolyline
    Dim ThisDrawing As AcadDocument
    Dim Pt1 As Variant
    Dim LWArea As Double, LWZ As Double

    ' Autocad Session handling
    On Error Resume Next
    Set ACAD = GetObject(, "AutoCAD.Application")
    On Error GoTo 0
    If ACAD Is Nothing Then
        Set ACAD = New AcadApplication
        ACAD.Visible = True
    End If
    Set ThisDrawing = ACAD.ActiveDocument
         
   
    ' select LwPolyline
    On Error Resume Next
    Do
        Err.Clear
        ThisDrawing.Utility.GetEntity LWPoly, Pt1, "Select a Polyline:"
    Loop While Err
    On Error GoTo 0
   
   
    'get LWPoly data
    With LWPoly
        LWArea = .Area
        LWZ = .Elevation
    End With
   
    ' write LWPoly data on worksheet
    Set MyCell = ActiveCell
    With MyCell
        .Offset(0, 0).Value = "Area:"
        .Offset(0, 1).Value = LWArea
        .Offset(1, 0) = "Z:"
        .Offset(1, 0) = LWZ
    End With
   
    Set ThisDrawing = Nothing
    Set ACAD = Nothing
   
 End Sub
--- End code ---

bye

MSTG007:
Way cool. Can I have the user select a bunch of polylines at once?

RICVBA:
Sure. The simplest way could be enclose in a "do while" loop the code lines between "'select LWPolyline" to the 2nd "end with". With a proper "while" condition.
Otherwise you could add a selectionset on screen and then loop through the LWPolylines gathered this way.

MSTG007:
Could I ask for an example if you do not mind? What you did is a huge stepping stone!

We have some polylines that have areas that we need to put into spreadsheets

Navigation

[0] Message Index

[#] Next page

Go to full version