Microsoft KB Archive/185368

= XL98: No Shortcut Keys Listed on Custom Menu Item =

Article ID: 185368

Article Last Modified on 6/17/2005

-

APPLIES TO


 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q185368



SYMPTOMS
If you run a macro that adds a custom menu item to a menu, the macro is run without error, but the shortcut key combination is not added next to the menu item. Also, the shortcut key for the menu item does not function.



CAUSE
Your shortcut key combination may not be displayed next to your custom menu item, and the shortcut key combination may not work if the macro that creates the menu item was created in Microsoft Excel for the Macintosh, version 5.0.

The object model for toolbars, menus and menu items has changed to the CommandBar object model in Microsoft Excel 98 Macintosh Edition. With this change, the Shortcutkey argument for the Add method of the MenuItems collection is being ignored.



WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following example utilizes the new CommandBars object model to add a custom menu to the worksheet menu bar. This custom menu has one menu item added to it, and this menu item has a shortcut key combination listed next to it.

Creating and Running the Macro
 Close and save any open workbooks, and then create a new workbook. Start the Visual Basic Editor (press OPTION+F11). On the Insert menu click module.  Enter the following in the module: Sub Test 'This macro is called by the "my item" menu item on the '"my menu" menu. MsgBox "your custom menu item" End Sub

Sub Add_Menu

'This will add a menu item called "my menu" to the menu bar. Set mymenu = CommandBars("worksheet menu bar").Controls.Add _ (Type:=msoControlPopup) mymenu.Caption = "my menu"

'This will add a menu item called "my item" to the "my menu" 'menu. Set myitem = mymenu.Controls.Add(Type:=msoControlButton) myitem.Caption = "my item" myitem.OnAction = "Test" myitem.ShortcutText = "Cmd+Option+k" myitem.Style = 2

End Sub  On the File menu, click "Close and Return to Microsoft Excel." On the Tools menu, point to Macro, and then click Macros.</li> Select Test in the list of macros, and then click Options.</li> Type the letter "k" in the box for the Shortcut key. Click OK.</li> Select add_menu in the list of macros, and then click Run.

The custom menu item is added to the menu bar.</li> Press COMMAND+OPTION+K.

The Test macro is run, and a message box is displayed.</li> Click OK.</li></ol>

Removing the Custom Menu
<ol> Start the Visual Basic Editor (press OPTION+F11).</li>  Type the following code on the module you created in the previous steps: Sub Remove_Menu

CommandBars("worksheet menu bar").Controls("my menu").Delete

End Sub </li> Run the Remove_Menu macro.</li></ol>

The custom menu is removed from the menu bar.

<div class="status_section">

STATUS
Microsoft has confirmed this to be a problem in Microsoft Excel 98 Macintosh Edition.

<div class="moreinformation_section">

MORE INFORMATION
The following macros successfully run in both Excel 5.0 and Excel 98. However, in Excel 98, the custom menu item does not have the shortcut key displayed next to it.

NOTE: In Excel 5.0, the custom menu item, Item1, can be activated by pressing COMMAND+K. Sub Add_Menu

Application.MenuBars(xlWorksheet).Menus.Add "Test" Set mymenu = MenuBars(xlWorksheet).Menus("Test").MenuItems mymenu.Add Caption:="Item1", OnAction:="Select_Item1", _ ShortcutKey:="k"

End Sub

Sub Select_Item1 MsgBox "Item1" End Sub Run the following macro to remove the custom menu created by the Add_Menu macro from above. Sub Remove_Test Application.MenuBars(xlWorksheet).Menus("Test").Delete End Sub

<div class="references_section">