How to create a menu and add a MacroItem


The CreateMenu VBA macro shows how to add a new menu to the Main Menubar in ArcMap and add items to it. The ArcMap Add Data command is added to the new menu. Then the sample macro MyMacro is added as item on the new menu.
All programmatic customizations are temporary. If you programmatically customize ArcMap, these changes will only appear while the current document is open in the current ArcMap session. Programmatic changes are never saved in the document or templates. Once you close that document or shutdown ArcMap, the changes are removed. If you are customizing ArcCatalog, these changes will only appear during the current ArcCatalog session.

How to use

  1. Paste this code into the Normal ThisDocument code window in the Visual Basic Editor in ArcMap.
  2. Run the CreateMenu macro.
  3. A new menu called MyMenu should appear on the menubar. This menu should have two items on it.
[VBA]
Public Sub CreateMenu()
    ' Find the MainMenuBar.
    Dim pMainMenuBar As ICommandBar
    Set pMainMenuBar = Application.Document.CommandBars.Find(ArcID.MainMenu)
    
    ' Create the new menu called "MyMenu" on the MainMenuBar.
    Dim pNewMenu As ICommandBar
    Set pNewMenu = pMainMenuBar.CreateMenu("MyMenu")
    
    ' Add a built in command to the new menu.
    ' The built in ArcID module is used to get the command ID for the AddData
    ' command.
    pNewMenu.Add ArcID.File_AddData
    
    ' Add a macro to the new menu.
    ' The macro item's name will be "MyMacroItem", it'll have an icon of a happy
    ' face (the 1st bitmap image), and will fire a macro called "MyMacro" (provided
    ' below).
    pNewMenu.CreateMacroItem "MyMacroItem", 1, "Normal.ThisDocument.MyMacro"
End Sub


Public Sub MyMacro()
    ' This is the macro that is added to the new menu.
    MsgBox ("Hey, I know how to create a new menu and a new macro item! Cool!")
End Sub