XL97: WE1183: "Customizing Menu Bars, Menus, and Menu Items"

ID: Q166755

The information in this article applies to:

SUMMARY

The Application Note "Customizing Menu Bars, Menus, and Menu Items" (WE1183) is now available from Microsoft Product Support Services. This Application Note can help you learn techniques for writing Visual Basic for Applications code to customize menus in Microsoft Excel 97. This Application Note is meant to be used as a supplement to Chapter 8 ("Menus and Toolbars") of the "Microsoft Office 97/Visual Basic Programmer's Guide." This Application Note contains code examples you can run. These code examples can be used with the following elements: menu bars, menus, menu items, submenus, and shortcut menus.

To Obtain This Application Note

The following file is available for download from the Microsoft Software Library:

 ~ We1183.exe (size: 58041 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from Online Services

MORE INFORMATION

The Text of We1183

                             TABLE OF CONTENTS
                             =================

Overview
   Using the Examples in This Application Note

Visual Basic for Applications Code Examples and Tips
   Command Bars
   Control Constants
   Menu Bars
      Returning an ID for a Command Bar Control
      Saving the Active State (for Built-in or Customized Menu Bars)
      Creating a Custom Command Bar
      Displaying a Custom Command Bar
      Deleting a Custom Command Bar
      Hiding a Command Bar
      Displaying a Command Bar
      Restoring a Built-in Command Bar
   Menus
      Adding a Custom Menu Control to a Command Bar
      Disabling a Menu Control on a Command Bar
      Enabling a Menu Control on a Command Bar
      Deleting a Menu Control on a Command Bar
      Restoring a Menu Control on a Command Bar
   Menu Items
      Adding a Separator Bar to a Menu Control
      Creating a Custom Menu Item Control on a Menu
      Placing a Check Mark Next to a Menu Item Control
      Disabling a Menu Item Control on a Command Bar
      Enabling a Menu Item Control on a Command Bar
      Deleting a Menu Item Control on a Menu
      Restoring a Built-in Menu Item Control on a Menu
   Submenus
      Adding a Submenu
      Adding a Menu Item to a Submenu
      Disabling a Menu Item Control on a Submenu
      Deleting a Command on a Submenu
      Disabling a Submenu Control
      Deleting a Submenu Control
   Shortcut Menu Bars
      Creating a New Shortcut Menu Bar
   Shortcut Menus
      Creating a Menu Item on a Shortcut Menu Bar
      Disabling a Menu Item Control on a Shortcut Menu Bar
      Deleting a Menu Item on a Shortcut Menu Bar
      Deleting a Shortcut Menu Bar
      Restoring a Menu Item on Built-in Shortcut Menu Bar
   Submenus on Shortcut Menus
      Creating a New Submenu on a Shortcut Menu Bar
      Creating a Menu Item Control on a Submenu Located on a Shortcut Menu
      Bar
      Disabling a Submenu Item Control on a Shortcut Menu
      Deleting a Submenu Item Control on a Shortcut Menu
      Disabling a Submenu Control on a Shortcut Menu
      Deleting a Submenu Control on a Shortcut Menu

Where to Find More Information
   The Object Browser
   Microsoft Knowledge Base
      Using the Microsoft Knowledge Base on the World Wide Web
      Using the Microsoft Knowledge Base on The Microsoft Network

                                 OVERVIEW
                                 ========

To perform many of the common tasks that are associated with customizing menu bars and menus in Microsoft Excel 97 for Windows, use the Customize dialog box (click Customize on the Tools menu). To perform more advanced tasks, or to tailor menu bars and menus for a custom application, you may want to create Visual Basic for Applications procedures.

For more information about using the Customize dialog box, click the Office Assistant, type "customize menubar" (without the quotation marks), click Search, and then click to view a topic.

This Application Note can help you learn techniques for writing Visual Basic for Applications code for customizing menu bars, menus, menu items, submenus, and shortcut menus.

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft Support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

                USING THE EXAMPLES IN THIS APPLICATION NOTE
                ===========================================

The examples in this Application Note use Visual Basic for Applications code in Microsoft Excel 97 to customize menus. To use the example macros, follow these steps:

1. In Microsoft Excel 97, open the Visual Basic Editor. To do this, point

   to Macros on the Tools menu and click Visual Basic Editor (or press
   ALT+F11).

2. In the Visual Basic Editor, insert a Visual Basic module sheet. To do
   this, click Module on the Insert menu.

3. Type one of the macro examples from this Application Note. If you
   downloaded this document from an online location, you can copy and paste
   the example directly into a module sheet. You can then run the example
   in the workbook.

4. To run a macro, point to Macro on the Tools menu, and then click Macros.
   In the Macro dialog box, click the macro name and click Run.

           VISUAL BASIC FOR APPLICATIONS CODE EXAMPLES AND TIPS
           ====================================================

Command Bars

In Microsoft Excel 97, menu bars and toolbars are referred to as the same programmable object type, the CommandBar object. Within the CommandBar object are the controls that you use to refer to menus, menu items, submenus, and shortcut menus. With each control, you use a constant in the Type argument to specify which type of control to use for the menu, submenu, or menu item. For example, the MsoControlButton constant refers to menu items on menus. This Application Note discusses customizing and controlling command bar menus using Visual Basic for Applications.

Control Constants

The following is a list of the various control constants that specify the type of graphical control to use for a particular menu bar control:

MsoControlButton               MsoControlGraphicDropdown
MsoControlButtonDropdown       MsoControlGraphicPopup
MsoControlButtonPopup          MsoControlGrid
MsoControlComboBox             MsoControlLabel
MsoControlCustom               MsoControlOCXDropDown
MsoControlDropdown             MsoControlPopup
MsoControlEdit                 MsoControlSplitButtonMRUPopup
MsoControlExpandingGrid        MsoControlSplitButtonPopup
MsoControlGauge                MsoControlSplitDro pdown
MsoControlGenericDropdown      MsoControlSplitExpandingGrid
MsoControlGraphicCombo

Manipulating menus in Microsoft Excel primarily involves using the constants MsoControlButton (menu commands), MsoControlPopUp (menus), and MsoBarPopup (shortcut menu bars). For a graphical explanation of each constant, click the Office Assistant in the Visual Basic Editor, type "property" (without the quotation marks), click Search, and then click to view the "Type Property (Microsoft Office Reference)" topic.

Menu Bars

A menu bar, which is a kind of command bar, is the kind of object on which you add menus, menu items, and submenus.

The Office Assistant in Microsoft Office Visual Basic Help can help you find information about the topics in the following table.

   For Information About This Topic              Search for
   -------------------------------------------------------------

   To add a new menu bar                         Add menubar
   To activate a created or built-in menu bar    Show menubar
   To delete a custom menu bar                   Delete menubar
   To determine whether a menu is built-in       Builtin menubar
   To restore all built-in menu bars, menus,     Restore menubar
   and menu items

The built-in menu bars that are included in Microsoft Excel 97 are listed in the following table.

   Built-in Menu Bar             Description
   ------------------------------------------------------------------------

   Worksheet menu bar            The menu bar that appears when a worksheet
                                 is activated
   Chart menu bar                The menu bar that appears when a chart
                                 sheet or a chart object is selected

Returning an ID for a Command Bar Control:

Each command bar control has a unique ID. To return an ID for a control, use a macro.

This following example returns the ID for the Tools menu control on the Chart menu bar:

   Sub Id_Control ()
   Dim myId as Object
      set myId = CommandBars("Chart Menu Bar").Controls("Tools")
      MsgBox myId.Caption & Chr(13) & MyId.Id
   End Sub

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q159466
   TITLE     : XL97: List of ID Numbers for Built-In Command Bar Controls

Saving the Active State (for Built-in or Customized Menu Bars):

You may want to declare OriginalMenuBar a public variable so that a subroutine can use it in an another subroutine, such as an Auto_Close subroutine. Declaring and using the variable this way resets the user's previous menu bar to its original state. The following sample macro resets the menu bar:

   Public OriginalMenuBar as Object

   Sub MenuBars_Capture()
      Set OriginalMenuBar = CommandBars.ActiveMenuBar
   End Sub

The following sample macro recovers the captured menu bar:

   Sub MenuBars_Recover()
      OriginalMenuBar.Visible = True
   End Sub

Determining the Name of the Active Menu Bar:

The following sample macro returns the name of the active menu bar:

   Sub MenuBars_GetName()
      MsgBox CommandBars.ActiveMenuBar.Name
   End Sub

Creating a Custom Command Bar:

The following example creates a custom command bar named My Command Bar:

   Sub MenuBar_Create()
      Application.CommandBars.Add Name:="My command bar"
   End Sub

NOTE: The command bar is empty because no controls were added to it. It does not appear until you display it.

Displaying a Custom Command Bar:

The following example creates and then displays a custom My Custom Bar menu bar, and then replaces the built-in menu bar:

   Sub MenuBar_Show()
   Dim myNewBar as Object
      ' Setting the menubar property to True replaces the built-in menu
      ' bar.
      Set myNewBar = CommandBars.Add (Name:="My Custom Bar", MenuBar:=True)
      ' You must first enable your custom menu bar before you make it
      ' visible.
      ' Enabling a menu bar adds it to the list of available menu bars on
      ' the Customize dialog box.
      MyNewBar.Enabled = True
      MyNewBar.Visible = True
   End Sub

NOTE: The menu bar is empty because no controls were added to it. See the "Menu Items" section for examples that add controls to menu bars.

Deleting a Custom Command Bar:

The following example deletes the custom menu bar named My Custom Bar that you created in the previous example:

   Sub MenuBar_Delete()
      CommandBars("My Custom Bar").Delete
   End Sub

NOTE: You can only disable built-in command bars; you cannot delete them.

Hiding a Command Bar:

The following example hides the built-in Worksheet menu bar:

   Sub MenuBar_Disable()
      CommandBars("Worksheet Menu Bar").Enabled = False
   End Sub

Displaying a Command Bar:

The following example displays the built-in Worksheet menu bar:

   Sub MenuBar_Display()
      CommandBars("Worksheet Menu Bar").Enabled = True
   End Sub

Restoring a Built-in Command Bar:

Restoring a menu bar resets the default controls (menus and menu items). The following example restores the built-in Worksheet menu bar:

   Sub MenuBar_Restore()
      CommandBars("Worksheet Menu Bar").Reset
   End Sub

NOTE: You can reset only built-in menu bars; you cannot reset a custom menu bar.

Menus

You can add menus to a custom or built-in menu bar.

The Office Assistant in Microsoft Office Visual Basic Help can help you find information about the topics in the following table.

   For Information About This Topic              Search for
   ----------------------------------------------------------

   To add a menu to the worksheet menu bar       Add menu
   To delete a menu from the worksheet menu bar  Delete menu
   To restore a deleted built-in menu on the     Builtin menu
   worksheet menu bar
   To disable a menu on the worksheet menu bar   Disable menu

Adding a Custom Menu Control to a Command Bar:

The following example adds the myMenu menu to the Worksheet menu bar:

   Sub Menu_Create()
   Dim myMnu As Object
      Set myMnu = CommandBars("worksheet menu bar").Controls. _
         Add(Type:=msoControlPopup, before:=3)
      With myMnu
      ' The "&" denotes a shortcut key assignment (Alt+M in this case).
         .Caption = "my&Menu"
      End With
   End Sub

Disabling a Menu Control on a Command Bar:

A menu that is disabled appears dimmed and is not available.

The following example disables the File menu:

   Sub Menu_Disable()
      CommandBars("Worksheet menu bar").Controls("File").Enabled = False
   End Sub

Enabling a Menu Control on a Command Bar:

The following example enables the File menu that you disabled in the previous example:

   Sub Menu_Enable()
      CommandBars("Worksheet menu bar").Controls("File").Enabled = True
   End Sub

Deleting a Menu Control on a Command Bar:

The following example deletes the Help menu from the Worksheet menu bar:

   Sub Menu_Delete()
      CommandBars("Worksheet menu bar").Controls("Help").Delete
   End Sub

Restoring a Menu Control on a Command Bar:

To restore a menu control, you must know the identification (ID) number for the control. To determine the ID number, see the "Returning an ID for a Command Bar Control" section.

The following example restores the Help menu on the Worksheet menu bar:

   Sub Menu_Restore()
   Dim myMnu As Object
      Set myMnu = CommandBars("Worksheet menu bar")
      ' Id 30010 refers to the Help menu control.
      myMnu.Controls.Add Type:=msoControlPopup, Id:=30010, before:=10
      myMnu.Reset
   End Sub

Menu Items

You can add menu item controls to or remove them from built-in or custom menu controls on command bars. Each command bar control has a unique identification number (ID). An ellipsis (...) that follows a menu item caption denotes a menu item that opens a dialog box. Menu items with a small, black arrow after the caption display one or more submenu items.

The Office Assistant in Microsoft Office Visual Basic Help can help you find information about the topics in the following table.

   For Information About This Topic              Search for
   ----------------------------------------------------------

   To add a menu item to an existing worksheet   Add menuitem
   menu
   To delete a menu item                         Delete menuitem
   To disable a custom menu item                 Disable menuitem

Adding a Separator Bar to a Menu Control:

A separator bar is a horizontal line that separates menu commands on a menu.

The following example adds a separator bar before the Worksheet command on the Insert menu:

   Sub menuItem_AddSeparator()
      CommandBars("Worksheet menu bar").Controls("Insert") _
      .Controls("Worksheet").BeginGroup = True
   End Sub

NOTE: To remove a separator bar, set the BeginGroup property to False.

Creating a Custom Menu Item Control on a Menu:

The following example creates a new command called Custom1 on the Tools menu of the Worksheet menu bar, and then runs the macro Code_Custom1 when you click Custom1:

   Sub menuItem_Create()
      With CommandBars("Worksheet menu bar").Controls("Tools")
         .Controls.Add(Type:=msoControlButton, Before:=1).Caption = _
         "Custom1"
         .Controls("Custom1").OnAction = "Code_Custom1"
      End With
   End Sub

NOTE: This is the code that runs when you click Custom1.

   Sub Code_Custom1()
      MsgBox "you clicked Custom1"
   End Sub

Placing a Check Mark Next to a Menu Item Control:

The following example places a check mark next to the Custom1 command if it is not selected and removes the check mark if it is selected:

   Sub menuItem_checkMark()
   Dim myPopup as Object

      Set myPopup = CommandBars("Worksheet menu bar").Controls("Tools")
      If myPopup.Controls("Custom1").State = msoButtonDown Then
         ' Remove check mark next to menu item.
         myPopup.Controls("Custom1").State = msoButtonUp
         MsgBox "Custom1 is now unchecked"
         Else
           ' Add check mark next to menu item.
            myPopup.Controls("Custom1").State = msoButtonDown
            MsgBox "Custom1 is now checked"
       End If
      End Sub

Disabling a Menu Item Control on a Command Bar:

Menu items that are disabled appear dimmed and are not available.

The following example disables the Custom1 menu item that you created on the Tools menu:

   Sub MenuItem_Disable()
   Dim myCmd as Object
      Set myCmd = CommandBars("Worksheet menu bar").Controls("Tools")
      myCmd.Controls("Custom1").Enabled = False
   End Sub

Enabling a Menu Item Control on a Command Bar:

The following example enables the Custom1 menu item that you disabled in the previous example:

   Sub MenuItem_Enable()
   Dim myCmd as Object
      Set myCmd = CommandBars("Worksheet menu bar").Controls("Tools")
      myCmd.Controls("Custom1").Enabled = True
   End Sub

Deleting a Menu Item Control on a Menu:

The following example deletes the Save menu item on the File menu:

   Sub menuItem_Delete()
   Dim myCmd as Object
      Set myCmd = CommandBars("Worksheet menu bar").Controls("File")
      MyCmd.Controls("Save").Delete
   End Sub

Restoring a Built-in Menu Item Control on a Menu:

To restore a menu item control, you must know the identification (ID) number for the control. To determine the ID number, see the "Returning an ID for a Command Bar Control" section.

The following example restores the Save menu item that you deleted in the previous example:

   Sub menuItem_Restore()
   Dim myCmd as Object
      Set myCmd = CommandBars("Worksheet menu bar").Controls("File")
      ' Id 3 refers to the Save menu item control.
      myCmd.Controls.Add Type:=msoControlButton, Id:= 3, Before:=5
   End Sub

Submenus

Submenus appear to the side of the parent menu when you click a menu item. A menu item that is a submenu control has a small black arrow located at the right end of the menu item name.

Adding a Submenu:

The following example adds a new submenu named NewSub to the Tools menu on the Worksheet menu bar:

   Sub SubMenu_Create()
   Dim newSub as Object
      Set newSub = CommandBars("Worksheet menu bar").Controls("Tools")
      With newSub
         .Controls.Add(Type:=msoControlPopup, Before:=1).Caption="NewSub"
      End With
   End Sub

Adding a Menu Item to a Submenu:

This example adds a new menu item named SubItem1 to the NewSub submenu, and then runs the Code_SubItem1 macro when you click SubItem1:

   Sub SubMenu_AddItem()
   Dim newSubItem as Object
      Set newSubItem = CommandBars("Worksheet menu bar") _
      .Controls("Tools").Controls("NewSub")
      With newSubItem
         .Controls.Add(Type:=msoControlButton, Before:=1).Caption = _
         "SubItem1"
         .Controls("SubItem1").OnAction = "Code_SubItem1"
      End With
   End Sub

The following is the macro code that runs when you click SubItem1:

   Sub Code_SubItem1()
      MsgBox "you clicked SubItem1"
   End Sub

Disabling a Menu Item Control on a Submenu:

Menu items that are disabled appear dimmed and are not available.

The following example disables the SubItem command that you created in the previous example:

   Sub SubMenu_DisableItem()
      CommandBars("Worksheet menu bar").Controls("Tools") _
      .Controls("NewSub").Controls("SubItem1").Enabled = False
   End Sub

NOTE: To enable the disabled control, set the Enabled property to True.

Deleting a Command on a Submenu:

The following example deletes the SubItem1 command that you created on the NewSub submenu:

   Sub SubMenu_DeleteItem()
      CommandBars("Worksheet menu bar").Controls("Tools") _
      .Controls("NewSub").Controls("SubItem1").Delete
   End Sub

Disabling a Submenu Control:

Submenus that are disabled appear dimmed and are not available.

The following example disables the NewSub submenu that you created on the Tools menu:

   Sub SubMenu_DisableSub()
      CommandBars("Worksheet menu bar").Controls("Tools") _
      .Controls("NewSub").Enabled = False
   End Sub

NOTE: To enable the disabled control, set the Enabled property to True.

Deleting a Submenu Control:

The following example deletes the NewSub submenu that you created on the Tools menu:

   Sub SubMenu_DeleteSub()
      CommandBars("Worksheet menu bar").Controls("Tools") _
      .Controls("NewSub").Delete
   End Sub

Shortcut Menu Bars

Shortcut menu bars appear when you use the right mouse button to click a specific Microsoft Excel object. Microsoft Excel has many shortcut menu bars for which a variety of menus are available. You can also create custom shortcut menu bars and customize the built-in menu bars. The only difference between shortcut menus and other menu bars is that when you create the shortcut menu using the Add method, you must specify the msoBarPopUp constant as the Position argument. Use the ShowPopup method to display shortcut menus, as demonstrated in the previous examples. You can assign event procedures for user actions, for example, to display a shortcut menu in response to a right-click event.

The shortcut menu bars that are available in Microsoft Excel are as follows:

   &Wizard            Document                   Query and Pivot
   ActiveX Control    Excel Control              Query Layout
   AutoCalculate      Floor and Walls            Rotate Mode
   AutoFill           Formula Bar                Row
   Built-in Menus     Inactive Chart             Series
   Cell               Layout                     Shapes
   Chart              Nondefault Drag and Drop   System
   Column             Object/Plot                Title Bar (Charting)
   Connector          OLE Object                 Trendline
   Curve              Pictures Context           Menu WordArt Context Menu
   Curve Node         PivotTable Context Menu    Workbook Tabs
   Curve Segment      Plot Area                  XLM Cell
   Desktop            Ply
   Dialog             Query

Creating a New Shortcut Menu Bar:

The following example creates a new shortcut menu bar called myShortcutBar:

   Sub Shortcut_Create()
   Dim myShtCtBar as Object
      Set myShtCtBar = CommandBars.Add(Name:="myShortcutBar", _
      Position:=msoBarPopup)
      ' This displays the shortcut menu bar.
      ' 200, 200 refers to the screen position in pixels as x and y
   coordinates.
      myShtCtBar.ShowPopup 200,200
   End Sub

NOTE: The shortcut menu bar appears empty because no controls (menu items or submenus) have been added to it yet.

The constant msoBarPopup is used in the Position argument to denote a shortcut menu bar.

Shortcut Menus

Creating a Menu Item on a Shortcut Menu Bar:

The following example creates a new menu command named Item1 on the shortcut menu bar named myShortcutBar and runs the Code_Item1 macro when you click Item1:

   Sub Shortcut_AddItem()
   Dim myBar as Object
      Set myBar = CommandBars("myShortcutBar")
      With myBar
         .Controls.Add (Type:=msoControlButton, before:=1).Caption = _
         "Item1"
         .Controls("Item1").OnAction = "Code_Item1"
      End With
      myBar.ShowPopup 200,200
   End Sub

The following is the macro that runs when you click Item1 on myShortcutBar:

   Sub Code_Item1()
      MsgBox "you clicked Item1"
   End Sub

Disabling a Menu Item Control on a Shortcut Menu Bar:

Menu items that are disabled appear dimmed and are not available.

The following example disables the Item1 command that you created in the previous example:

   Sub Shortcut_DisableItem()
      Set myBar = CommandBars("myShortcutBar")
      myBar.Controls("Item1").Enabled = False
      myBar.ShowPopup 200,200
   End Sub

NOTE: To enable the disabled item, set the Enabled property to True.

Deleting a Menu Item on a Shortcut Menu Bar:

The following example deletes the menu command named Item1 on myShortcutBar:

   Sub Shortcut_DeleteItem()
      Set myBar = CommandBars("myShortcutBar")
      myBar.Controls("Item1").Delete
      myBar.ShowPopup 200,200
   End Sub

Deleting a Shortcut Menu Bar:

Deleting the shortcut menu bar removes it and all of its items. You cannot restore a deleted custom menu bar. To restore it, you must recreate it and all of its menu items, submenus, and so on.

The following example deletes the shortcut menu myShortCutBar that you created in a previous example:

   Sub Shortcut_DeleteShortCutBar()
      CommandBars("MyShortCutBar").Delete
   End Sub

Restoring a Menu Item on Built-in Shortcut Menu Bar:

You can restore default menu items on built-in menu bars. However, you cannot restore custom menu items. To restore a built-in menu control, you must know the identification (ID) number for the control. To determine the ID number, see the "Returning an ID for a Command Bar Control" section.

The following example restores the Cut command on the worksheet Cell shortcut menu bar:

   Sub Shortcut_RestoreItem()
      ' Id 21 refers to the Help menu control.
      CommandBars("Cell").Controls.Add Type:=msoControlButton, Id:21,
   Before:=1
   End Sub

Submenus on Shortcut Menus

You can create submenus on shortcut menu bars. Submenus appear to the side of the parent menu when you click a menu item control. A menu item that is a submenu control has a small, black arrow located to the right of its name.

Creating a New Submenu on a Shortcut Menu Bar:

The following example adds a new submenu named NewSub on the worksheet Cell shortcut menu:

   Sub ShortcutSub_Create()
      CommandBars("Cell").Controls.Add(Type:=msoControlPopup, before:=1) _
      .Caption = "NewSub"
       ' This displays the shortcut menu bar.
       ' 200, 200 refers to the screen position in pixels as x and y
       ' coordinates.
      CommandBars("Cell").ShowPopup 200, 200
   End Sub

NOTE: The submenu is empty because no menu items have been added to it yet.

Creating a Menu Item Control on a Submenu Located on a Shortcut Menu Bar:

The following macro adds the command subItem1 to the submenu NewSub that you created on the Cell shortcut menu, and then runs the Code_subItem1 macro when you click subItem1:

   Sub ShortcutSub_AddItem()
   Dim newSubItem as Object
      Set newSubItem = CommandBars("Cell").Controls("NewSub")
      With newSubItem
         .Controls.Add(Type:=msoControlButton, before:=1).Caption =
          "subItem1"
         ' This will run the subItem1_Code macro when subItem1 is clicked.
         .Controls("subItem1").OnAction = "Code_subItem1"
      End With
      ' This displays the Cell shortcut menu bar.
      ' 200, 200 refers to the screen position in pixels as x and y
      ' coordinates
      CommandBars("Cell").ShowPopup 200, 200
   End Sub

The following is the macro that runs when you click subItem1:

   Sub subItem1_Code()
      MsgBox "you clicked subItem1"
   End Sub

Disabling a Submenu Item Control on a Shortcut Menu

Menu items that are disabled appear dimmed and are not available.

The following example disables the command subItem1 on the NewSub submenu:

   Sub ShortcutSub_DisableItem()
      CommandBars("Cell").Controls("NewSub") _
      .Controls("subItem1").Enabled = False
      ' This displays the Cell shortcut menu bar.
      ' 200, 200 refers to the screen position in pixels as x and y
      ' coordinates.
      CommandBars("Cell").ShowPopup 200, 200
   End Sub

NOTE: To enable a disabled item, set the Enabled property to True.

Deleting a Submenu Item Control on a Shortcut Menu:

The following example deletes the command subItem1 on the NewSub submenu:

   Sub ShortcutSub_DeleteItem()
      CommandBars("Cell").Controls("NewSub").Controls("subItem1").Delete
      ' This displays the Cell shortcut menu bar.
      ' 200, 200 refers to the screen position in pixels as x and y
      ' coordinates.
      CommandBars("Cell").ShowPopup 200, 200
   End Sub

Disabling a Submenu Control on a Shortcut Menu:

Menu items that are disabled appear dimmed and are not available.

The following example disables the NewSub submenu on the Cell shortcut menu bar:

   Sub ShortcutSub_DisableSub()
      CommandBars("Cell").Controls("NewSub").Enabled = False
      ' This displays the Cell shortcut menu bar.
      ' 200, 200 refers to the screen position in pixels as x and y
      ' coordinates.
      CommandBars("Cell").ShowPopup 200, 200
   End Sub

NOTE: To enable a disabled item, set the Enabled property to True.

Deleting a Submenu Control on a Shortcut Menu:

Deleting the submenu removes it and all of its items. You cannot restore a deleted custom submenu. To restore it, you must recreate it and all of its menu items, submenus, and so on.

The following example deletes the NewSub submenu that you created on the Cell shortcut menu bar:

   Sub ShortcutSub_DeleteSub()
      CommandBars("Cell").Controls("NewSub").Delete
      ' This displays the Cell shortcut menu bar.
      ' 200, 200 refers to the screen position in pixels as x and y
      ' coordinates.
      CommandBars("Cell").ShowPopup 200, 200
   End Sub

WHERE TO FIND MORE INFORMATION

The Object Browser

The Object Browser contains a complete list of all of the properties and methods for a specific menu item. To find this information, switch to the Visual Basic Editor (press ALT+F11), click Object Browser on the View menu (or press F2), type the name of the control in the search box, and then press ENTER or click Search.

For more information about using the Object Browser, click the Office Assistant in the Visual Basic Editor, type "object browser" (without the quotation marks), click Search, and then click to view the "Use the Object Browser" topic.

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Excel Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

ARTICLE-ID: Q120802

TITLE     : Office: How to Add/Remove a Single Office Program or Component

Microsoft Knowledge Base

The Microsoft Knowledge Base is a primary Microsoft product information source for Microsoft Technical Support professionals. The Microsoft Knowledge Base is also available to Microsoft customers. This comprehensive database contains detailed articles with technical information about Microsoft products, documented fix lists, documentation errors, and answers to commonly asked technical support questions. These articles are available through the World Wide Web, The Microsoft Network, CompuServe*, Genie(tm), and the Microsoft Developer Network compact disc. You can also access the Knowledge Base if you purchase a Microsoft TechNet compact disc subscription.

Using the Microsoft Knowledge Base on the World Wide Web:

To connect to the Microsoft Knowledge Base on the Web, use the following address

   http://support.microsoft.com/support

and follow the detailed instructions on the page.

NOTE: Because the Microsoft Web site is constantly updated, the site address may change without notice. If this occurs, link to the Microsoft home page at the following address:

   http://www.microsoft.com

Using the Microsoft Knowledge Base on The Microsoft Network:

To connect to the Microsoft Knowledge Base, use the following steps:

1. On the Edit menu, click Go to.

2. Click Other Location.

3. When the prompt "Type a Go word for a particular service" appears, type

   "mssupport" (without the quotation marks).

NOTE: It may take a minute or more for the Microsoft Knowledge Base to appear while it prepares the necessary files to run.

4. Double-click MS Knowledge Base.

The disk and software contained on it, including any accompanying documentation (the "Software"), are provided to you at no additional charge. Microsoft Corporation owns all rights, title, and interest in and to the Software. The user assumes the entire risk as to the accuracy and the use of the Software.

COPYRIGHT NOTICE. Copyright (c) 1997 Microsoft Corporation. Microsoft and/or its suppliers, One Microsoft Way, Redmond, Washington 98052-6399 U.S.A. All rights reserved.

TRADEMARKS. Microsoft, Windows, Windows NT, MSN, The Microsoft Network and/or other Microsoft products referenced herein are either trademarks or registered trademarks of Microsoft. Other product and company names mentioned herein may be the trademarks of their respective owners.

The names of companies, products, people, characters and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product or event, unless otherwise noted.

NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND, AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NON- INFRINGEMENT, WITH REGARD TO THE SOFTWARE.

LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL MICROSOFT OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THE SOFTWARE, EVEN IF MICROSOFT HAS BEEN ADVISED OF THE POSSIBLITY OF SUCH DAMAGES. BECAUSE SOME STATES AND JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY. MICROSOFT'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY UNDER THIS EULA SHALL NOT EXCEED FIVE DOLLARS (US$5.00).

The following conditions also apply to your use of the Software:

The Software may be copied and distributed internally only, subject to the following conditions: All | text must be copied without modification and all pages must be included; If software is included, all files on the disk(s) |must be copied without modification [the MS-DOS(R) utility diskcopy is appropriate for this purpose]; All components of this Software must be distributed together; and This Software may not be distributed to any third party.

If you are not a Microsoft Premier customer, Microsoft shall not provide technical support for this Software.

The Software is provided with RESTRICTED RIGHTS. Use, duplication, or disclosure by the Government is subject to restrictions set forth in subparagraph (c)(1)(ii) of the Rights in Technical Data and Computer Software clause at DFARS 252.227-7013 or subparagraphs (c)(1) and (2) of the Commercial Computer Software-Restricted Rights at 48 CFR 52.227-19, as applicable. Manufacturer is Microsoft Corporation, One Microsoft Way, Redmond, WA 98052-6399. Any transfer of the Software must be accompanied by this statement and may only be transferred if first approved by Microsoft.

You agree that you will not export or re-export the Software to any country, person, entity or end user subject to U.S.A. export restrictions, and you are responsible for complying with all applicable U.S. and local export laws in connection with the use of this Software. You warrant and represent that neither the U.S.A. Bureau of Export Administration nor any other federal agency has suspended, revoked or denied you export privileges.

This EULA is governed by the laws of the State of Washington, U.S.A.

Additional query words: appnote

Keywords          : kbappnote kbfile kbprg kbdta KbVBA 
Version           : WINDOWS:97
Platform          : WINDOWS
Issue type        : kbinfo

Last Reviewed: May 17, 1999