How can I refer to AutoCAD block from Excel VBA

How can I refer to AutoCAD block from Excel VBA

Anonymous
Not applicable
4,450 Views
3 Replies
Message 1 of 4

How can I refer to AutoCAD block from Excel VBA

Anonymous
Not applicable

Hi all. I having autocad project where is 1 dynamic block which I'm trying to change from excel. Here is vba script which I'm using to change block:

 

 

Dim dybprop As Variant, i As Integer
Dim bobj As AcadEntity

For Each bobj In ACADApp.ModelSpace
    If bobj.ObjectName = "AcDbBlockReference" Then
        If bobj.IsDynamicBlock Then
            If bobj.EffectiveName = "AdjBlock" Then
                dybprop = bobj.GetDynamicBlockProperties
                For i = LBound(dybprop) To UBound(dybprop)
                    If dybprop(i).PropertyName = "Distance1" Then
                        dybprop(i).Value = 50.75
                        Acad.Application.Update
                    End If
                Next i
            End If
        End If
    End If
Next

When I'm running it in AutoCAD VBA it works excellent. Than I'm creating Excel VBA project and copying this code. Before running it I creating connection to existing AutoCad project like this:

 

On Error Resume Next

   Dim ACADApp As AcadApplication
   Dim a As Object

   Set a = GetObject(, "AutoCAD.Application")

   If a Is Nothing Then
      Set a = CreateObject("AutoCAD.Application")

      If a Is Nothing Then
         MsgBox "AutoCAD must be running before performing this action.", vbCritical
         Exit Sub
      End If
   End If

   Set ACADApp = a

   Set ACADApp.ActiveDocument = ACADApp.Documents.Open("c:\KIRILL\Programming\Drawing1_VBATest.dwg")

When I'm running it from Excel VBA - AutoCAD project appears but nothing change. Honestly I don't have any idea why in Excel VBA it doesn't works while in AutoCAD it work. May be somebody had this problem before? Thanks in advance.

P.S. Full Excel VBA code:

Sub Button9_Click()

  On Error Resume Next

   Dim ACADApp As AcadApplication
   Dim a As Object

   Set a = GetObject(, "AutoCAD.Application")

   If a Is Nothing Then
      Set a = CreateObject("AutoCAD.Application")

      If a Is Nothing Then
         MsgBox "AutoCAD must be running before performing this action.", vbCritical
         Exit Sub
      End If
   End If

   Set ACADApp = a

   Set ACADApp.ActiveDocument = ACADApp.Documents.Open("c:\KIRILL\Programming\Drawing1_VBATest.dwg")

Dim dybprop As Variant, i As Integer
Dim bobj As AcadEntity

For Each bobj In ACADApp.ModelSpace
    If bobj.ObjectName = "AcDbBlockReference" Then
        If bobj.IsDynamicBlock Then
            If bobj.EffectiveName = "AdjBlock" Then
                dybprop = bobj.GetDynamicBlockProperties
                For i = LBound(dybprop) To UBound(dybprop)
                    If dybprop(i).PropertyName = "Distance1" Then
                        dybprop(i).Value = 50.75
                        Acad.Application.Update
                    End If
                Next i
            End If
        End If
    End If
Next



End Sub
0 Likes
Accepted solutions (1)
4,451 Views
3 Replies
Replies (3)
Message 2 of 4

Ed__Jobe
Mentor
Mentor
Accepted solution

Possibly?

For Each bobj In ACADApp.ModelSpace

Should be:
For Each bobj In ACADApp.ActiveDocument.ModelSpace

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 3 of 4

Anonymous
Not applicable

Yes... Oh thanks a lot. I spent a lot to solve this problem yet it was just inattentiveness Robot Sad

0 Likes
Message 4 of 4

Ed__Jobe
Mentor
Mentor

Just noticed that Acad.Application should be AcadApp. You haven't declared a var called Acad.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes