Excel 365 VBA GetObject, CreateObject Not Working For Solidworks 2022

Viewed 169

I'm responsible for some homegrown document control software at my company. This software is built in Excel and interacts with other Office products as well as Solidworks. It has been stable and working normally until now. We recently updated to Solidworks 2022 from 2021, and ever since the update, it appears that Excel no longer recognizes Solidworks as being open via GetObject, and then it tries to CreateObject but gets hung up, and the user receives the “Microsoft Excel is waiting for another application to complete an OLE action” error. I've verified that the correct libraries are selected after the update, and I don't really know what else to do here.

At the bottom of the post is a scrubbed snip of an example of some code that references Solidworks. All this does is open the source file for a given document number. The sub is given a file path that is generated from a search box. The code verifies it is actually a file path and then looks for the file extension. The issue is when it is a Solidworks file extension. Other Office products still work. Once the code reaches "Set swApp = GetObject(, "SldWorks.Application")", it is supposed to look for an active instance of Solidworks. Previously it would do this without any issue and proceed to OpenDoc6 and work as expected. Now, regardless of whether Solidworks is open or not, it jumps down to StartNew at the bottom, and it attempts to open a new instance with CreateObject. I worked with IT and found that if I ran Excel as admin, it would at least do this properly, but it would not recognize a running instance of Solidworks at all regardless of admin status, so it always jumps to StartNew.

I'm mostly self-taught with VBA and programming in general, so I'm definitely at the limits of my knowledge. I don't know what changed from SW 2021 to 2022 that would have affected this. Any help here would be greatly appreciated.

EDIT: I didn't do this initially, but I commented out the "Error GoTo StartNew" and received runtime error 429, ActiveX component can't create object. Not sure if this is relevant or not.

EDIT 2: I figured out a workaround. When I change "SldWorks.Application" to "SldWorks.Application.30" which is specifying SW 2022 (version number 22 + 8), everything works like before. Not sure why the generic "SldWorks.Application" callout does not work anymore though, and I would rather not hard-code a version number if I can avoid it.

Sub ViewSourceFile(FileName As String)
    
    Application.StatusBar = "* * * * * * * * * * * LOADING FILE! * * * * * * * * * * * * * *"

    Dim FileExtension$, FileExtLocation%, Command$
    
    If InStr(FileName, "\") = 0 Then
        ErrorMsg FileName, "file not found", True, False, False
        Application.StatusBar = ""
        Exit Sub
    End If
    
    FileExtLocation = InStrRev(FileName, ".")
    If FileExtLocation = 0 Then
        ErrorMsg FileName, "file not found", True, False, False
        Application.StatusBar = ""
        Exit Sub
    End If
    FileExtension = UCase(Mid(FileName, FileExtLocation + 1))
    
    Select Case FileExtension
     Case "SLDDRW", "SLDASM", "SLDPRT"
        Dim swApp As SldWorks.SldWorks
        Dim fileerror As Long
        Dim filewarning As Long
        Dim swModeldoc As SldWorks.ModelDoc2
        Dim SldType%
        
        Const swOpenDocOptions_Silent = 1
        Const swDocPART = 1
        Const swDocASSEMBLY = 2
        Const swDocDRAWING = 3
        
        If FileExtension = "SLDDRW" Then
            SldType = swDocDRAWING
        ElseIf FileExtension = "SLDPRT" Then
            SldType = swDocPART
        Else
            SldType = swDocASSEMBLY
        End If
        
        On Error GoTo StartNew
        Set swApp = GetObject(, "SldWorks.Application")
        On Error GoTo 0
        swApp.Visible = True
        
        Set swModeldoc = swApp.OpenDoc6(FileName, SldType, 0, "", fileerror, filewarning)
        
        If swModeldoc Is Nothing Then
            If fileerror = 2 Then
                ErrorMsg FileName, "file not found", True, False, False
            Else
                ErrorMsg FileName, "open document failed", True, False, False
            End If
        End If
     Case "XLS", "XLSX", "XLSM"
        Command = """" & GetCommand("Z3") & """ """ & FileName & """"
        Shell Command, vbNormalFocus
     Case "DOC", "DOCX"
        Command = """" & GetCommand("Z2") & """ """ & FileName & """"
        Shell Command, vbNormalFocus
     Case "PRT"
        Command = """" & GetCommand("Z1") & """ """ & FileName & """"
        Shell Command, vbNormalFocus
     Case "PPT", "PPTX"
        Command = """" & GetCommand("AX2") & """ """ & FileName & """"
        Shell Command, vbNormalFocus
     Case Else
        ErrorMsg FileName, "source file type not supported", True, False, False
    End Select
    AddToLogFile FileName, "Source File Viewed"
    
    Application.StatusBar = ""
    
    Exit Sub

StartNew:

    Set swApp = CreateObject("SldWorks.Application")
    Resume Next

End Sub
0 Answers
Related