MenuItem Object

Description

Represents an item on a menu (either a command or a separator bar).

Accessors

The MenuItem object is a member of the MenuItems collection. The MenuItems collection contains all of the menu items (the commands and command separators) on a menu. Use the Add method to create a new menu item and add it to a menu. Use the AddMenu method to add a submenu. To access a single member of the collection, use the MenuItems method with the index number or caption of the menu item as an argument.

The menu item index number indicates its position on the menu. MenuItems(1) is the item at the top of the menu; MenuItems(MenuItems.Count) is at the bottom. Remember that menu item separators (lines across the menu) are included in the collection of menu items. The following example adds a check mark to the left of menu item one on the Help menu of the Visual Basic Module menu bar.


MenuBars(xlModule).Menus("help").MenuItems(1).Checked = True

Use the Caption property to set or return the menu item caption. The following example adds a new menu item to the Help menu of the Visual Basic Module menu bar and then disables the menu item.


MenuBars(xlModule).Menus("help").MenuItems.Add "More Help"
MenuBars(xlModule).Menus("help").MenuItems("More Help").Enabled = False

The following example creates a table on worksheet one that contains the captions of all the menu items on all the menus on the Worksheet menu bar. The column headings are the menu captions, and the column entries under each heading are the menu item captions on that menu.


Sub EnumerateMenuItems()
    Worksheets(1).Activate
    c = 1
    For Each mn In MenuBars(xlWorksheet).Menus
        r = 1
        Cells(r, c) = mn.Caption
        r = r + 1
        For Each mi In mn.MenuItems
            Cells(r, c) = mi.Caption
            r = r + 1
        Next
        c = c + 1
    Next
End Sub

Properties

Application Property, Caption Property, Checked Property, Creator Property, Enabled Property, HelpContextID Property, HelpFile Property, Index Property, OnAction Property, Parent Property, StatusBar Property.

Methods

Delete Method.