Microsoft Office 2000/Visual Basic Programmer's Guide   

Creating a Command Bar

You can create toolbars by using the Customize dialog box or by using VBA code in any Office application. In Access, you can also create menu bars and pop-up menus by using the Customize dialog box. However, in all other Office applications, you must use VBA code to create menu bars or pop-up menus.

You create a custom command bar by using the CommandBars collection's Add method. The Add method creates a toolbar by default. To create a menu bar or pop-up menu, use the msoBarMenuBar or msoBarPopup constant in the Add method's Position argument. The following code sample illustrates how to create all three types of CommandBar objects:

Dim cbrCmdBar       As CommandBar
Dim strCBarName     As String

' Create a toolbar.
strCBarName = "MyNewToolbar"
Set cbrCmdBar = Application.CommandBars.Add(Name:=strCBarName)

' Create a menu bar.
strCBarName = "MyNewMenuBar"
Set cbrCmdBar = Application.CommandBars _
   .Add(Name:=strCBarName, Position:=msoBarMenuBar)

' Create a pop-up menu.
strCBarName = "MyNewPopupMenu"
Set cbrCmdBar = Application.CommandBars _
   .Add(Name:=strCBarName, Position:=msoBarPopup)

The CBCreateCommandBar procedure, available in the modCommandBarCode module in CommandBarSamples.mdb in the ODETools\V9\Samples\OPG\Samples\CH06 subfolder on the Office 2000 Developer CD-ROM, illustrates one way that you can use a single procedure to create all three command bar types.

After you have created a command bar, you still need to add any controls that you want and set the command bar's Visible property to True. For more information about adding controls to command bars, see "Working with Command Bar Controls" later in this chapter.