【发布时间】:2018-11-23 13:54:48
【问题描述】:
我有一个 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 中它起作用。可能有人以前遇到过这个问题吗?提前致谢。
附:完整的 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
【问题讨论】:
-
您尝试过任何基本调试吗?在循环内添加断点并检查对象?即使是基本的
Debug.Print语句也可以确定代码进入循环的程度?首先将On Error Resume Next更改为On Error Goto 0 -
这条线...
Acad.Application.Update- 你为什么不使用你的ACADApp对象? -
您不能只添加对 Autocad 库的引用并避免调用 GetObject 吗?并按照 cmets,使用调试器并一步一步地进行。
标签: vba excel autocad autocad-plugin