如何参考AutoCAD块

问题描述 投票:2回答:1

我有一个autocad项目,其中有一个动态块,我试图从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中工作时不起作用。以前可能有人有这个问题吗?提前致谢。

附:完整的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
excel vba excel-vba autocad autocad-plugin
1个回答
0
投票

您是否尝试添加参考库?

你可以去:

工具 - >参考

并添加:

[AutoCAD 20xx类型库]

© www.soinside.com 2019 - 2024. All rights reserved.