AnsweredAssumed Answered

Task not waiting for macro execution?

Question asked by Felix Sigwart on Jan 14, 2019
Latest reply on Jan 15, 2019 by Felix Sigwart

Hey everyone,

we have a task setup to run a custom VB.Net macro. With SolidWorks 2018 came the support for VSTA 3, to which I upgraded the called macro. It seems that SolidWorks is no longer waiting for the execution of the macro and closes the opened document immediately. Do you have the same behaviour? Can I configure SolidWorks to wait again?

 

The script in my task is pretty straight forward:

- Determine filetype

- Open file

- Run macro (swApp.RunMacro2(...))

- close file

Sub main()
    Set swApp = Application.SldWorks

swApp.Visible = True


docFileName = "<Filepath>"

' Determine type of SolidWorks file based on file extension
    If LCase(Right(docFileName, 7)) = ".sldprt" Or LCase(Right(docFileName, 4)) = ".prt" Then
        docType = swDocPART
    ElseIf LCase(Right(docFileName, 7)) = ".sldasm" Or LCase(Right(docFileName, 4)) = ".asm" Then
        docType = swDocASSEMBLY
    ElseIf LCase(Right(docFileName, 7)) = ".slddrw" Or LCase(Right(docFileName, 4)) = ".drw" Then
        docType = swDocDRAWING
    Else
        docType = swDocNONE
         If bIsSupportedExtension(Mid(docFileName, InStrRev(docFileName, ".") + 1)) = False Then
             Log "The file extension '" & Mid(docFileName, InStrRev(docFileName, ".") + 1) & "' is not supported."
             Exit Sub
         End If        
    End If
        
    ' Open document
    If docType = swDocNONE Then
        Set swModel = swApp.LoadFile4(docFileName, "", Nothing, errors)
        docType = swModel.GetType
    Else  
        Set swDocSpecification = swApp.GetOpenDocSpec(docFileName)
        swDocSpecification.DocumentType = docType 
        swDocSpecification.ReadOnly = True
        swDocSpecification.Silent = True
        swDocSpecification.ConfigurationName = ""
        swDocSpecification.DisplayState = ""
        Set swModel = swApp.OpenDoc7(swDocSpecification)
        errors = swDocSpecification.Error


    End If
    
    If errors = swFutureVersion Then
        Log "Document '" & docFileName & "' is future version."
        Exit Sub
    End If


    ' Load failed?
    If swModel Is Nothing Then
        Log "Method call ModelDoc2::OpenDoc7 for document '" & docFileName & "' failed. Error code " & errors & " returned."
        Exit Sub
    End If


boolstatus = swApp.RunMacro2("*PathToTheMacro*\Macro1.dll", "Macro1", "main", 1, errors)

    ' Close document
    swApp.QuitDoc swModel.GetTitle
End Sub

 

Example macro:

Partial Class SolidWorksMacro
    Public Sub main()


        MsgBox("foo")


    End Sub
    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks
End Class

 

In the old version SolidWorks called the macro and waited for the execution. In this example it would wait for the user to click the Messagebox and then close the file and SolidWorks.

SolidWorks waits with the closing of the file and executes the macro if I place a MessageBox after swApp.RunMacro2(...).

 

I could make a workaround and let the task loop over a check if the macro is finished. But I'd prefer a solution without a possible inifinity loop.

 

Thanks for your help.

Outcomes