如何参考AutoCAD块

时间:2018-06-13 21:20:49

标签: excel vba excel-vba autocad autocad-plugin

我有autocad项目,其中有1个动态块,我正在尝试从excel更改。 这是我用来改变块的vba脚本:

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 With

当我在AutoCAD VBA中运行它时,效果非常好。比我正在创建Excel VBA项目并复制此代码。在运行之前,我创建了与现有AutoCad项目的连接,如下所示:

  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")

当我从Excel VBA运行它时 - AutoCAD项目出现但没有任何变化。老实说,我不知道为什么在Excel VBA中它在AutoCAD中工作时不起作用。以前可能有人有这个问题吗?提前谢谢。

P.S。完整的Excel VBA代码:

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

1 个答案:

答案 0 :(得分:0)

您是否尝试过添加参考库?

您可以转到:

  

工具->参考

并添加:

  

[ AutoCAD 20xx类型库]