ShortcutText on Menu Items

CommandBarButtons have a ShortcutText property that you can use to list a command’s shortcut next to its menu item. The Cut menu item has Ctrl+X listed next to it.

In this example, I make a new menu with one item under it. OnKey is used to assign a keyboard shortcut and the ShortcutText property is used to communicate that to the user. Note that using the ShortcutText property does not actually establish the keyboard shortcut.

Sub MakeMenu()
   
    Dim cbPop As CommandBarPopup
   
    DeleteMainMenu
   
    Set cbPop = Application.CommandBars(1).Controls.Add(msoControlPopup)
   
    Application.OnKey “^+C”, “Choice1”
   
    With cbPop
        .Caption = “MainMenu”
        With .Controls.Add(msoControlButton)
            .Caption = “Choice1”
            .OnAction = “Choice1”
            .ShortcutText = “Ctrl+Shift+C”
        End With
    End With
   
End Sub

And to get rid of it:

Sub DeleteMainMenu()
   
    On Error Resume Next
        Application.CommandBars(1).Controls(“MainMenu”).Delete
        Application.OnKey “^+C”
       
End Sub

Another way to assign a shortcut key is through MacroOptions. It’s the same as Tools > Macro > Macros, then selecting the Option button.

Application.MacroOptions “Choice1”, , , , True, “C”

I always use OnKey, but I’m not aware of any problems with this method. The “C” argument means Ctrl+Shift+C. All of the shortcuts established with MacroOptions start with Ctrl. You establish the Shift part by specifying a capital or lowercase C.

Posted in Uncategorized

3 thoughts on “ShortcutText on Menu Items

  1. Oh god, I can’t believe I’ve never noticed that property before! So far I’ve been doing:
    .Caption = “Choice1 Ctrl+Shift+C”

    I feel stupid now :> Thanks for the tip!

  2. And why would you expect any different? Article published in 2008. So, if you have question – ask.
    From where I stand experts doing great job and helped loot of people on the way.


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published. Required fields are marked *