Creating a Custom Toolbar

To create a custom toolbar, menu bar, or shortcut menu, use the Add method of the CommandBars object.

Suppose you want to create a new toolbar called "Budget Plans." The Add method that you need to invoke looks as follows:

CommandBars.Add(Name, Position, MenuBar, Temporary)

The optional argument Name is the name you want to assign to your new command bar. If you don't specify the name, Visual Basic assigns a generic name, such as "Custom1."

The Position argument determines where the new command bar will appear on the screen (see Table 12-2).

Table 12-2: Position constants for the CommandBar object

Position Constant Index Description

0 Command bar is docked on the left side of the application window.

2 Command bar is docked on the right side of the application window.

1 Command bar is docked at the top of the application window.

3 Command bar is docked at the bottom of the application window.

4 Command bar is floating (undocked).

5 Command bar is a shortcut menu.

6 Command bar replaces the system menu bar (Macintosh only).

The MenuBar argument is a logical value (True or False) that determines whether or not the new command bar replaces the active menu bar. Enter True if you want to replace the active menu bar; otherwise, use False.

The Temporary argument is a logical value (True or False) that determines when the command bar is deleted. Use True to have the command bar automatically deleted when the Excel application is closed. Using False will not remove the toolbar when you exit the program.

You can practice creating a toolbar in the Immediate window.

1. Enter the following statement in the Immediate window. Be sure to enter the complete statement on one line:

set newToolbar = ComrrandBars.Add("Budget Plans", msoBarRight, False, True)

When you press Enter, Visual Basic will add to the CommandBars collection a new toolbar with the specified name Budget Plans. To see this toolbar, switch to the Microsoft Excel application window and choose View | Toolbars. Excel displays a list of available toolbars, including the one you've just created (Figure 12-2).

2. Switch back to the Visual Basic Editor window, and enter the following statement in the Immediate window:

CommandBars("Budget Plans").Visible = True

To see the toolbar, switch to the Microsoft Excel application window. The Budget Plans toolbar is located to the right of the vertical scroll bar. Recall that when you created this toolbar, you used the msoBar-Right constant to determine its location.

3. Now close the Microsoft Excel application, and then activate it again and check whether the Budget Plans toolbar still appears on the right side of the application window. Because you have used the logical value of True in the position of the last argument of the Add method, the Budget Plans toolbar should be gone.

msoBarLeft msoBarRight msoBarTop msoBarBottom msoBarFloating msoBarPopup msoBarMenuBar

Msobarpopup Vba
Figure 12-2: A custom toolbar is added to the list of the built-in toolbars.

Before you attempt to create a new toolbar, it's a good idea to check whether or not a toolbar with the specified name already exists in the CommandBars collection. The following procedure creates the Budget Plans toolbar, provided there is no existing command bar with such a name. Enter this procedure in the Code window of the CustomTools (Chap12.xls) project, and run it twice. The second time you execute this procedure, you should see a message notifying you that such a toolbar already exists.

Sub MakeToolBar()

Dim bar As CommandBar Dim flagExists As Boolean flagExists = False

For Each bar In CommandBars If bar.Name = "Budget Plans" Then flagExists = True

MsgBox "The toolbar with this name already exists." Exit For End If Next bar

If Not flagExists Then

Set bar = CommandBars.Add("Budget Plans", _

msoBarBottom, False, True) ComrrandBars("Budget Plans").Visible = True End If Set bar = Nothing

End Sub

0 0


  • emilie
    How to position multiple toolbars using msobar?
    8 years ago

Post a comment