问题描述
我有一个用户在 2018 年从网上复制了一个宏。升级到2020后宏过不了第一行;
Set swApp = Getobject(,"Application.SldWorks")
错误是
运行时错误 '429' ActiveX 组件无法创建对象
我尝试重置库并浏览了多个论坛帖子以寻找解决方案。 This post 最接近我的问题。
以下是完整代码;
Dim swApp As SldWorks.SldWorks
Public Sub main()
Set swApp = Getobject(,"Application.SldWorks")
Dim ActiveDoc As ModelDoc2
Set ActiveDoc = Getobject(,"Sldworks.Application").ActiveDoc
If Not ActiveDoc Is nothing Then
If ActiveDoc.GetType = 2 Then
GoTo Traverse
End If
End If
MsgBox ("This macro should be run,with an open assembly as the active document.")
Exit Sub
Traverse:
Dim myModel As ModelDoc2
Set myModel = ActiveDoc
Call Traverse(myModel,myModel.ConfigurationManager.ActiveConfiguration.Name)
MsgBox ("Done")
End Sub
解决方法
您不应该使用像 ActiveDoc
这样的保留名称作为变量名。您不需要在主机已经直接引用的对象上使用 GetObject
。您不再需要 Call
。
如果我使用您在那里的 GetObject
命令,我只会收到您看到的错误。
我已在 SolidWorks 2020 中测试过此代码:
Option Explicit
Dim swApp As Object
Sub main()
Set swApp = Application.SldWorks
Dim thisDoc As ModelDoc2
Set thisDoc = swApp.ActiveDoc
If Not thisDoc Is Nothing Then
If thisDoc.GetType = 2 Then
Dim myModel As ModelDoc2
Set myModel = thisDoc
Traverse myModel,myModel.ConfigurationManager.ActiveConfiguration.Name
MsgBox "Done"
Exit Sub
End If
End If
MsgBox "This macro should be run,with an open assembly as the active document."
End Sub