vbaautodesk-inventor

Run a VBA routine from VBA IDE?


I want to get a list of routines from a VBA project, then run the macros selected by the user.

The image below shows the native "Macros" box. I want to extend this functionality to multiple macros across multiple documents.
enter image description here

I found this link which solves the first part of the problem. Now that I have my list, how do I run a selected routine by name?


Solution

  • Hello and welcome to SO

    Below is code sample how to execute VBA macro using code. You need to add some form to select documents and macros for execute. This depends on your implementation.

    
    Sub RunMacroUsingCode()
        Dim vbaProjectName As String
        vbaProjectName = "InventorVBA"
        
        Dim vbaModuleName As String
        vbaModuleName = "m_Tests"
        
        Dim vbaMacroName As String
        vbaMacroName = "RunMultipleMacrosTestCall"
        
        Dim vbaProject As InventorVBAProject
        For Each vbaProject In ThisApplication.VBAProjects
            If vbaProject.name = vbaProjectName Then Exit For
        Next
        
        Dim vbaModule As InventorVBAComponent
        For Each vbaModule In vbaProject.InventorVBAComponents
            If vbaModule.name = vbaModuleName Then Exit For
        Next
        
        'Using result is optional
        Dim result As Variant
        Call vbaModule.InventorVBAMembers(vbaMacroName).Execute(result)
        
    End Sub
    
    Function RunMultipleMacrosTestCall()
        Call MsgBox("TEST")
        RunMultipleMacrosTestCall = True
    End Function