English

How To: Run a VBA macro using IVbaApplication::RunVBAMacro

Summary

The VBA sample in this article demonstrates how to run and pass arguments to a macro using the IVbaApplication::RunVBAMacro command. The arguments are passed using a Variant array.

Procedure

  1. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.


  2. Create a new Code Module.

    Select Module from the Insert menu.

  3. Copy this sample code into the Code Module:

    Code:
    Sub RunMacro()
    Dim pVbaApp As IVbaApplication
    Set pVbaApp = Application
    Dim myArray As Variant
    'enter name and age
    myArray = Array("Jane Doe", 34)
    pVbaApp.RunVBAMacro "Project", "Module1", "MyMacro", myArray
    End Sub

    Sub MyMacro(Name As String, Age As Integer)
    MsgBox "Your name is " & Name & " and you are " & Age & " years old."
    End Sub

  4. Run the Sub procedure.

    Select Run Sub/UserForm from the Run menu.