Adding and Displaying Shortcut Menus

A shortcut menu is a floating command bar that the user displays by right-clicking. It can contain the same control types as a command bar, and the controls behave in the same way as on a command bar. In most applications, however, you cannot create or modify shortcut menus from the application's interface. Therefore, you need to create and modify your shortcut menus at run time.

Adding shortcut menus at run time

The only difference between shortcut menus and other toolbars is that when you create the shortcut menu with the Add method, you must specify msoBarPopUp as the Position argument. The following example creates a new shortcut menu, adds two controls (with captions) to it, and then uses the ShowPopup method to display the new menu.

Set copyAndPasteMenu = CommandBars.Add( _
    Name:="Custom", Position:=msoBarPopup, _
    Temporary:=True)
Set copy = copyAndPasteMenu.Controls.Add
With copy
    .FaceId = CommandBars("Standard").Controls("Copy").Id
    .Caption = "Copy the selection"
End With
Set paste = copyAndPasteMenu.Controls.Add
With paste
    .FaceId = CommandBars("Standard").Controls("Paste").Id
    .Caption = "Paste from the Clipboard"
End With
copyAndPasteMenu.ShowPopup 200, 200

Displaying shortcut menus

Use the ShowPopup method to display shortcut menus, as demonstrated in the preceding example.

If the container application supports assigning event procedures to user actions, you can display a shortcut menu in response to a right-click event. However, not all applications support event procedures. Check the documentation for your container application to see whether the application supports event procedures.

Making run-time modifications to shortcut menus

Any changes you make to a shortcut menu must be made at run time, and the changes you make will generally be limited to changing the appearance or action of the controls on the menu. For more information about adding and managing menu items, see Adding and Managing Menu Bars and Menu Items .

See Also | Adding and Managing Menu Bars and Menu Items | Adding and Modifying Toolbars | Using Command Bars