1

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?

5
  • 1
    Application.Run? Commented Nov 9, 2021 at 5:21
  • Oh, interesting. This looks like an Excel thing, I'm writing in Autodesk Inventor but I have the excel library added already so it kinda works. However - it seems that Application.Run works with Functions, but not Sub Routines... Any suggestions? Commented Nov 9, 2021 at 6:01
  • It can run Sub (at least in excel) but I'm not sure in the context of using within Autodesk Inventor. Commented Nov 9, 2021 at 6:12
  • Nope - Application.Run "TestSub" results in runtime error 1004 (macro not available) Commented Nov 9, 2021 at 6:38
  • I assume it doesn't work because the macro is not in Excel instance so unfortunately this won't work for your case. Commented Nov 9, 2021 at 6:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's perfect - thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.