Add a menu to the menu bar and populate it with commands

Article contributed by Bill Coan

Add a menu called MyMenu to the menu bar, then add several commands to the menu. In this case, all of the commands do the same thing: they run a macro called TestMacro.

Before running this code, take a minute to make a macro called TestMacro. Otherwise, the commands on the new menu will generate an error when Word realizes that the commands are trying to run a non-existent macro.

Code
This code is provided for illustrative purposes only and is not warranted to be suitable for any particular business purpose. The code may be freely copied for any lawful business purpose.

'specify where new menu will be stored
CustomizationContext = ActiveDocument.AttachedTemplate

'add a menu to the Menu Bar
With CommandBars("Menu Bar").Controls
    .Add(Type:=msoControlPopup, Before:=3).Caption = "&MyMenu"
End With

'add commands to the new menu
With CommandBars("Menu Bar").Controls("MyMenu").Controls

    Set myButton = .Add(Type:=msoControlButton)
    myButton.FaceId = 18
    myButton.Caption = "Run Test Macro"
    myButton.OnAction = "TestMacro"

    Set myButton = .Add(Type:=msoControlButton)
    myButton.FaceId = 23
    myButton.Caption = "Run Test Macro"
    myButton.OnAction = "TestMacro"

    Set myButton = .Add(Type:=msoControlButton)
    myButton.FaceId = 201
    myButton.Caption = "Run Test Macro"
    myButton.OnAction = "TestMacro"
    myButton.BeginGroup = True

    Set myButton = .Add(Type:=msoControlButton)
    myButton.FaceId = 208
    myButton.Caption = "Run Test Macro"
    myButton.OnAction = "TestMacro"

End With