Visual Basic Concepts

Command Bars: A Replacement for Menus and Toolbars

See Also

Even though menus are differentiated from toolbar buttons because they look different from each other, to Visual Basic they are in essence the same kind of object. Internally, Visual Basic considers menus, menu commands, toolbars, and toolbar buttons as command bars.

A command bar unifies the concept of menus and toolbars into a single common visual and programmatic object. As command bars, menus contain menu commands which can have icons and captions and exhibit button-like behavior while remaining in the familiar menu format. Some toolbar buttons have dropdown arrows, similar to ComboBox controls. A command bar object can contain other command bar objects, depending on its type.

While a discussion of the command bar object model is beyond the scope of this book, here are a few basics to help you place your add-in where you want it.

There are three types of command bars:

Popup A Popup command bar is equivalent to a menu item on a menu bar.
ComboBox A ComboBox command bar is similar to a ComboBox control. That is, a toolbar button with a dropdown arrow next to it (like the Add Project toolbar button). When you click the arrow, it displays more menu commands with icons.
Button A Button command bar is equivalent to a standard toolbar button. That is, a button with an icon displayed on it.

Getting to Command Bars Through Code

The concept of command bars is important to you really only as a programmer, since the interface acts more or less the same as it did in previous versions of Visual Basic. Programmatically, however, it is quite different. You can gain access to and explore the various command bar objects in the Object Browser by selecting the Microsoft Office 8.0 Object Library check box in the References dialog box.

Since menus and toolbars are in the same object library, they’re referred to through the CommandBarControl object. For example, to declare the basic object for use in your code, you would enter:

Dim mcbMenuCommandBar As Office.CommandBarControl

Every toolbar, menu, or context menu in the development environment is a command bar. It helps to understand command bars if you think of them as containers that hold other command bars. Therefore, a menu bar command bar can contain several menu items, each of which is itself a command bar, and each menu item can contain several menu commands, each of which is also a command bar.

Getting Your Bearings Among the Command Bars

This model makes placing your add-in in the development environment easy. You simply refer to the command bar in which you wish your add-in to appear. You refer to the command bars from top to bottom and left to right. In the example below, we expose our new add-in as a command on the Tools menu. Here’s an example of how to do this:

Set mcbMenuCommandBar = _
VBInst.CommandBars(1).Controls(8).CommandBar. _
Controls.Add(1, , , 3)

The code above installs a popup command bar before the third menu command from the top. What's in this code?

Working with the Model's Flexibility

Note, however, that command bar controls can be moved by users to any location within a menu or toolbar, which means you cannot rely on them to always remain in the same numeric position. Additionally, if you support localized versions of Visual Basic, you cannot rely on the command bar control captions to always remain the same.

However, there is a method that allows you to always obtain a specific menu item, regardless of its location. The name of the CommandBar object of the menu item is not localized; thus, you can always specify this name in the top-level CommandBars collection to refer to a menu item. For example, to obtain a reference object to the command bar representing the Add-Ins menu, you could use the following statement:

Set cmdBar = VBInst.CommandBars("Add-Ins")

This works regardless of the location of the menu item or the caption name. You obtain the CommandBar object of a particular menu item, then use its Controls collection to add an item to the menu.