|Create Menu and Add 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.
Note: 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:
- Paste this code into the Normal ThisDocument code window in the Visual Basic Editor in ArcMap.
- Run the CreateMenu macro.
- A new menu called MyMenu should appear on the menubar. This menu should have two
items on it.
Public Sub CreateMenu()
' Find the MainMenuBar.
Set pMainMenuBar = Application.Document.CommandBars.Find(ArcID.MainMenu)
' Create the new menu called "MyMenu" on the MainMenuBar.
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 AddDate
' 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
pNewMenu.CreateMacroItem "MyMacroItem", 1, "Normal.ThisDocument.MyMacro"
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!")