Creating Buttons for VBA Macros

What is a Macro?

The title for this posting is specific to a question that's come up a couple of times recently, but the post is actually more general and covers the many ways you can execute an Inventor VBA macro.

First, let's define what a macro is.  It's a public sub without any arguments within a standard code module in an Inventor VBA project.  The following code shows a valid macro.  It's declared as a Public sub with no arguments.

Public Sub SampleMacro()
    MsgBox "The macro is running."
End Sub

The following is also a valid macro since subs default to being Public if you don't specify it to be Public or Private.

Sub SampleMacro()
    MsgBox "The macro is running."
End Sub

The following is not a valid macro since it has arguments.

Public Sub AddNumbers(Value1 As Double, Value2 As Double)
    MsgBox "Value sum: " & Value1 + Value2
End Sub

Standard code modules are shown in the project window under the Modules node in the tree, as shown below.  You can have any number of code modules within a project and all new projects will contain a module named "Module1".  A macro can exist in any module.

AppProjectModule1

VBA's Project window and a code module.

 

Running a Macro Using the Macros Command

One interface you can use to run a VBA macro is the Macros command (Tools -> Macro -> Macros… or Alt-F8).  Running this command will display the dialog shown below.  This dialog displays a list of all of the available macros.  It lists them as ModuleName.MacroName.  The "Macros in" drop-down list let you select different VBA projects.  To run a macro, select it in the list and click the Run button.

MacroDialog

The Macros command dialog.

This interface is always available to run a macro but is not the most convenient or easiest to use, especially if you have macros that you frequently use.

Running a Macro Using a Button

For frequently used macros it's much more convenient to create a button to execute the macro.  To create a button for a macro there's one additional requirement on your macro.  It must exist in the default VBA project.  This is the VBA project that Inventor loads automatically every time it is started.  It's likely that your macro is already in this project.  The default VBA project is defined using the File tab of Application Options command, as shown below.  In this case it is Default.ivb in the Bin\Macros directory, but it can be any .ivb file.

DefaultVBAProject

Specifying the default VBA project.

 

To create a button use the Customize command (Tools -> Customize…) and select the Commands tab of the dialog.  In the Categories list there is a "Macros" category.  After selecting that you'll see a list of available macros in the Commands list on the right.  You can drag and drop any of the macros in the list onto any available toolbar, including the panel bar.  This is illustrated in the picture below.

CustomizeDialog

Using the Customize command to create a macro button.

 

It's easy to create a button for your macro but it will have a default icon that's used for all VBA macros.  If you have more than one macro button it can be difficult to tell which button is for which macro.  The tool tip for the button does display the macro name, however it's also possible to create your own icon for a macro button.

Icons are created using whatever bitmap editor you choose.  The Paint program that comes with Windows will work.  Create a bitmap that is 16×16 pixels in size.  It can use any colors but magenta is interpreted as the transparent color.  Below is an example.  The picture on the left shows the icon (zoomed in) within the Paint program.  The picture on the right shows the same icon used for a button.  Notice that the magenta color has become the background color.

IconEdit   IconSample

Icon samples.

 

To associate an icon with a macro you must name it correctly and put it in the correct location.  The rules for naming are:

   ModuleName.MacroName.Size.bmp

The ModuleName is the name of the VBA module where your macro is.  In this example it is "Module1".  The MacroName is the name of your macro.  In this example it is "SampleMacro".  The Size can be either "Small" or "Large".  Small icons are 16×16 pixels and large icons are 32×32 pixels.  Inventor allows the end-user to choose to use large or small icons.  If you only supply a small icon Inventor will scale it and create a large icon if needed.  For the example above you would create a 16×16 pixel bitmap called:

   Module1.SampleMacro.Small.bmp

The large bitmap name is:

   Module1.SampleMacro.Large.bmp

These files need to be located in the same directory as Default.ivb (or your default VBA project).  Inventor will automatically associate them with the macro when creating a button.  If your icon does not display then double-check the name and the size of the icon.

 

Running a Macro from a Program

Although not commonly used, it's also possible to run a macro from another program.  This isn't limited to running only macros, as defined above, but any function or sub in any project and module can be executed.  I won't go into a lot of detail since there is likely not much interest in this but here is an example that demonstrates the process.  First, here are two macros in Inventor's default VBA project.

Public Sub SampleMacro()
    MsgBox "The macro is running."
End Sub
Public Sub AddNumbers(Value1 As Double, Value2 As Double)
    MsgBox "Value sum: " & Value1 + Value2
End Sub

Here is an Excel macro that connects to Inventor and executes the two Inventor macros.

Public Sub RunInventorMacros()    
    ' Get a reference to Inventor.  This assumes Inventor is running.
     Dim invApp As Inventor.Application
    Set invApp = GetObject(, "Inventor.Application")

    ' Get a reference to the default VBA project.
    ' It will always be the first item in the collection.
    Dim invVBAProject As InventorVBAProject
    Set invVBAProject = invApp.VBAProjects.Item(1)

    ' Get a reference to the Module1 code module.
    Dim invModule As InventorVBAComponent
    Set invModule = invVBAProject.InventorVBAComponents.Item("Module1")

    ' Get a reference to the SampleMacro sub.
    Dim invSub As InventorVBAMember
    Set invSub = invModule.InventorVBAMembers.Item("SampleMacro")

    ' Execute the macro.
    invSub.Execute

    ' Get a reference to the AddNumbers sub.
    Set invSub = invModule.InventorVBAMembers.Item("AddNumbers")

    ' Set the arguments.
    invSub.Arguments.Item("Value1").Value = 3.5
    invSub.Arguments.Item("Value2").Value = 4.75

    ' Execute the sub
    Call invSub.Execute
End Sub


Comments

6 responses to “Creating Buttons for VBA Macros”

  1. Nice article. need more article on Macros.

  2. Thx Brian,
    I needed this and very clearly explained also!
    Jan van Noort,
    The Netherlands, Delft
    I’m building a Dutch forum on VBA subjects, still needs to grow ….

  3. Ralph Daub Avatar
    Ralph Daub

    Thank you. I was looking for information on how to change the macro icons on Inventor.
    This article was spot on!

  4. Hi,
    thanks a lot for this information. Tried to find this info for a while. Now my customer got a nice icon with his macro…
    Best wishes, Tom

  5. Craig Avatar
    Craig

    Brian. I am trying to this in IV2010 but it never displays the icons in any of the environments, always the default icon.
    Lets say i have a macro called “Sub ActiveStandard ()”. My icon is called Module3.ActiveStandard.Large.BMP and is located in my project location.
    What am i missing? Does the macro need to call the icon?

  6. Craig Avatar
    Craig

    Also i seen the other webpagepage and also have a small version with the filename Module3.ActiveStandard.Small.BMP 16x16pixels.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading