Excel Menubar Contrl VBA MicroSoft
Excel Menubar Contrl VBA MicroSoft
Page 1 of 15
Article ID: 830502 - Last Review: January 11, 2006 - Revision: 4.3
This article describes how to customize menus and menu bars in Microsoft Excel 2000 and later. This article contains step-by-step instructions and code samples to programmatically manage and customize menu bars, menus, commands, submenus, and shortcut menus in Microsoft Excel.
To perform many of the common tasks that are associated with customizing menu bars and menus in Microsoft Excel 2000, in Microsoft Excel 2002, and in Microsoft Office Excel 2003, use the Customize dialog box. To perform more advanced tasks, or to tailor menu bars and menus for a custom program, you may want to create Microsoft Visual Basic for Applications (VBA) codes. For more information about how to use the Customize dialog box, click Microsoft Excel Help on the Help menu, type customize menu bar in the Office Assistant or the Answer Wizard, and then click Search to view the topic. This article can help you learn techniques for writing VBA code for customizing menu bars, menus, menu items, submenus, and shortcut menus.
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.
http://support.microsoft.com/kb/830502
25/05/2010
Page 2 of 15
Command bars
In Microsoft Office, toolbars, menu bars, and shortcut menus are all controlled programmatically as one type of object: command bars. All the following items are represented in VBA by CommandBar objects: Menu bars, toolbars, and shortcut menus. Menus on menu bars and toolbars. Submenus on menus, submenus, and shortcut menus. You can modify any built-in menu bar or any built-in toolbar, and you can create and modify custom toolbars, menu bars, and shortcut menus to deliver with your VBA code. You present the features of your program as individual buttons on toolbars or as groups of command names on menus. Because toolbars and menus are both command bars, you use the same kind of controls. In VBA and in Microsoft Visual Basic, buttons and menu items are represented by CommandBarButton objects. The pop-up controls that display menus and submenus are represented by CommandBarPopup objects. In the following examples, the control that is named "Menu" and the control that is named "Submenu" are both pop-up controls that display a menu and a submenu. Both the menu and the submenu are unique CommandBar objects with their own set of controls. In Microsoft Excel, menu bars and toolbars are referred to as the same programmable object type, the CommandBar object. You use the controls in the CommandBar object to refer to menus, menu items, submenus, and shortcut menus. You use a constant with each control in the Type argument to specify the type of control that you want to use for the menu, the submenu, or the command.
Control constants
The following is a list of the various control constants in Excel 2003 that specify the type of graphical control to use for a particular menu bar control: MsoControlActiveX* MsoControlAutoCompleteCombo*** MsoControlButton MsoControlButtonDropdown MsoControlButtonPopup MsoControlComboBox MsoControlCustom MsoControlDropdown MsoControlEdit MsoControlExpandingGrid MsoControlGauge MsoControlGenericDropdown MsoControlGraphicCombo MsoControlGraphicDropdown MsoControlGraphicPopup MsoControlGrid MsoControlLabel MsoControlLabelEx*** MsoControlOCXDropDown MsoControlPane ** MsoControlPopup
http://support.microsoft.com/kb/830502
25/05/2010
Page 3 of 15
MsoControlSpinner*** MsoControlSplitButtonMRUPopup MsoControlSplitButtonPopup MsoControlSplitDropdown MsoControlSplitExpandingGrid MsoControlWorkPane** *=New in Microsoft Excel 2000 **= New in Microsoft Excel 2002 ***=New in Microsoft Office Excel 2003
Menu bars
A menu bar is a kind of command bar. A menu bar is the kind of object where you add menus, menu items, and submenus. For more information about how to manage menu bars and menu items in Excel, follow these steps: 1. 2. 3. 4. Start the Microsoft Visual Basic Editor. On the Help menu, click Microsoft Visual Basic Help. In the Office Assistant box or in the Answer Wizard box, type Menu bars, and then click Search. In Excel 2003 and in Excel 2002, click Adding and Managing Menu Bars and Menu Items. In Excel 2000, click About menus and toolbars. You can make modifications to both the menu bar and to the controls on that menu bar at run time. The changes that you make to the menu bar may affect the appearance or the position of the menu bar. Changes that you make to the controls depend on the control type. The following table lists the most common properties and the common methods for changing the state, the action, or the contents of a control:
http://support.microsoft.com/kb/830502
25/05/2010
Page 4 of 15
Sub Id_Control () Dim myId as Object set myId = CommandBars("Worksheet Menu Bar").Controls("Tools") MsgBox myId.Caption & Chr(13) & MyId.Id End Sub
Save the active state (for built-in or for customized menu bars)
You may want to declare the OriginalMenuBar variable a public variable so that a subroutine can use it in 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
You can also create a custom command bar by using the Temporary:=True argument. The Temporary:=True argument permits the command bars to automatically be reset when you quit Excel. The following code uses the Temporary:=True argument to create a custom command bar: Sub MenuBar_Create() Application.CommandBars.Add Name:="My command bar", Temporary:=True End Sub
http://support.microsoft.com/kb/830502
25/05/2010
Page 5 of 15
Sub MenuBar_Show() Dim myNewBar As Object Set myNewBar = CommandBars.Add(Name:="Custom1", Position:=msoBarFloating) ' 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. ' Setting the menubar property to True replaces the built-in menu bar. myNewBar.Enabled = True myNewBar.Visible = True End Sub
Note You can only reset built-in menu bars. You cannot reset a custom menu bar.
Menus
http://support.microsoft.com/kb/830502
25/05/2010
Page 6 of 15
Restoring a menu bar resets the default controls (for both menus and menu items). The following example code restores the built-in Chart menu bar: Sub MenuBar_Restore() CommandBars("Chart").Reset End Sub
Note You can only reset built-in menu bars. You cannot reset a custom menu bar.
http://support.microsoft.com/kb/830502
25/05/2010
Page 7 of 15
Commands
The range of modifications that you can make to a command depends on the control type. Generally, buttons are either enabled or are hidden. Edit boxes, drop-down list boxes, and combo boxes are more versatile in that you can add or delete items from the list. Additionally, you can determine the action that is performed by looking at the value of the items that you selected from the list. You can change the action of any control to a built-in function or to a custom function. The following table lists the most common properties of a control and the methods for changing the state, the action, or the contents of a control:
For more information about menus in Excel 2003 and in Excel 2002, follow these steps: 1. 2. 3. 4. Start the Visual Basic Script Editor. On the Help menu, click Microsoft Visual Basic Help. In the Search Help box, type menus, and then press ENTER. Click Adding and Managing Menu Bars and Menu Items (Office).
http://support.microsoft.com/kb/830502
25/05/2010
Page 8 of 15
http://support.microsoft.com/kb/830502
25/05/2010
Page 9 of 15
Submenus
Submenus appear to the side of the parent menu when you click a command. A command that is a submenu control has a small black arrow that is located at the right end of the command name.
Add a submenu
The following example code adds a new submenu that is 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
http://support.microsoft.com/kb/830502
25/05/2010
Page 10 of 15
The following example enables the same SubItem command: Sub SubMenu_DisableItem() CommandBars("Worksheet menu bar").Controls("Tools") _ .Controls("NewSub").Controls("SubItem1").Enabled = True End Sub
http://support.microsoft.com/kb/830502
25/05/2010
Page 11 of 15
Note To enable the disabled control, set the Enabled property to True.
Note The shortcut menu bar appears empty because no controls (menu items or submenus) have been added to it.
Shortcut menus
Shortcut menu bars appear when you use the right mouse button to click a specific Excel object. Excel has many shortcut menu bars for which a variety of
http://support.microsoft.com/kb/830502
25/05/2010
Page 12 of 15
menus are available. You can also create custom shortcut menu bars and customize the built-in menu bars.
Note To enable the disabled item, set the Enabled property to True.
http://support.microsoft.com/kb/830502
25/05/2010
Page 13 of 15
Note The submenu is empty because no menu items have been added to it.
http://support.microsoft.com/kb/830502
25/05/2010
Page 14 of 15
http://support.microsoft.com/kb/830502
25/05/2010
Page 15 of 15
Object Browser
The Object Browser contains a complete list of all the properties and all the methods for a specific command. 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.
APPLIES TO
Microsoft Office Excel 2003 Microsoft Excel 2002 Standard Edition Microsoft Excel 2000 Standard Edition
Microsoft Support
2010 Microsoft
http://support.microsoft.com/kb/830502
25/05/2010