Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I know this sounds like a relatively common issue but non of the common solutions have worked for me, and so I set out to ask my own question. I have a user who in 2018 copy and pasted a macro from the web to help with a job function. For the last two years this macro has worked but after being upgraded to 2020 the macro can't even get past the first line;

Set swApp = GetObject(,"Application.SldWorks")

The error thrown is a Run-time error '429' ActiveX component can't create object

I have tried to reset the libraries and have gone through multiple forum posts looking a solution but i have found nothing that encompasses the same subject, though this post was the closest to my exact issue. Any light that can be shed here is greatly appreciated. Below is the full code;

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
530 views
Welcome To Ask or Share your Answers For Others

1 Answer

You shouldn't use reserved names like ActiveDoc as a variable name. You don't need to use GetObject on objects that are already directly referenced by the host. You don't need Call anymore.

I only get the error that you see if I use the GetObject command that you have there.

I have tested this code in 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...