Home > Enterprise >  Run a VBA routine from VBA IDE?
Run a VBA routine from VBA IDE?

Time:11-10

I'll be honest, I'm not exactly sure what to ask here. But 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 essentially want to extend this functionality to multiple macros across multiple documents.

Macro Dialogue Box

I found this link which solves the first part of the problems, but now that I have my list, I'm not sure how to run a selected routine by name.

Any suggestions?

CodePudding user response:

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

  • Related