How to: Add and Modify Toolbars

Office Developer Reference

The use of CommandBars in some Microsoft Office applications has been superseded by the new Ribbon user interface. For more information, search help for the keyword "Ribbon."

All host applications have an extensive interface for adding and designing custom toolbars (adding built-in buttons, adding macros as buttons, even adding pop-up controls to toolbars). The design-time changes you'll usually make from Visual Basic code are ones that add or modify combo box controls. Otherwise, working with toolbars in code is almost completely limited to making run-time changes (such as changing the button state, changing the button appearance, changing the button action).

Making run-time modifications to toolbars

There are several modifications you can make to a toolbar at run time. One of these modifications is to change the state of a command bar button on the toolbar. Each button control has two active states: pressed (True) and not pressed (False). To change the state of a button control, use the appropriate constant for the State property, as explained in the table later in this topic.

Another modification you can make at run time is to change the appearance or action of a button. To change the appearance of a button but not its action, use the CopyFace and PasteFace methods. These methods are useful if you want to copy the face of a particular button to the Clipboard or import it into another application to change some of its features. Use the PasteFace method to transfer the button image from the Clipboard onto a specific button.

To change a button's action to a function you've developed, assign the custom procedure name to the button's OnAction property.

The following table lists the most common properties and methods for changing the state, appearance, or action of a button.

Property or method Description
CopyFace , PasteFace Copies or pastes the image on the face of a button. Use the CopyFace method to copy the face of the specified button to the Clipboard. Use the PasteFace method to paste the contents of the Clipboard onto the face of the specified button. The PasteFace method will fail if the Clipboard is empty. If the image on the Clipboard is too large for the button face, the image won't be scaled down.

Generally, it's more convenient to copy and paste a button face at design time, but you can also make changes to a button face at run time. You can also use the FaceId property to assign a different built-in button face to a button.

Id Specifies the value that represents the button's built-in functionality. For example, a button that copies highlighted text to the Clipboard has an Id value of 19.
State Specifies the appearance, or state, of the button. Can be one of the following MsoButtonState constants: msoButtonDown, msoButtonMixed, or msoButtonUp.
Style Specifies whether the button face displays its icon or its caption. Can be one of the following MsoButtonStyle constants: msoButtonAutomatic, msoButtonIcon, msoButtonCaption, msoButtonIconAndCaption, msoButtonIconAndCaptionBelow, msoButtonIconAndWrapCaption, msoButtonIconAndWrapCaptionBelow, or msoButtonWrapCaption.
OnAction Specifies the procedure to be run when the user clicks a button, displays a menu, or changes the contents of a combo box control.
Visible Specifies whether the control is to be displayed or hidden from the user.
Enabled Enables or disables a command bar; the name of a disabled command bar won't appear in the list of available command bars.

The following example creates a new command bar containing a single command bar button. By using the OnAction property of the CommandBarButton object, you can use the CopyFace method to copy the button face of the built-in Open button to the Clipboard, and then you can use the PasteFace method to paste the face onto the existing button. This changes the appearance of the command bar button at run time.

  Sub testAddModifyToolbars1()
Set myBar = CommandBars _
    .Add(Name:="ChangingButton", Position:=msoBarTop, _
    Temporary:=True)
myBar.Visible = True 
Set oldControl = myBar.Controls _
    .Add(Type:=msoControlButton, _
    ID:=CommandBars("Standard").Controls("Copy").Id)
oldControl.OnAction = "changeFaces"
End Sub

Sub changeFaces() Set newControl = CommandBars.FindControl _ (Type:=msoControlButton, _ ID:=CommandBars("Standard").Controls("Paste").Id) newControl.CopyFace Set oldControl = _ CommandBars("ChangingButton").Controls(1) oldControl.PasteFace End Sub

Adding and modifying combo box controls

Edit boxes, drop-down list boxes, and combo boxes are powerful new controls you can add to toolbars in your Visual Basic application. However, most container applications require that you use Visual Basic code to design these controls. To design a combo box control, you use the properties and methods described in the following table.

Property or method Description
Add Adds a combo box control to a command bar by specifying one of the following MsoControlType constants for the Type argument: msoControlEdit, msoControlDropdown, or msoControlComboBox.
AddItem Adds an item to the drop-down list portion of a drop-down list box or combo box. You can specify the index number of the new item in the existing list, but if this number is larger than the number of items in the list, the AddItem method fails.
Caption Specifies the label for the combo box control. This is the label that's displayed next to the control if you set the Style property to msoComboLabel.
Style Specifies whether the caption for the specified control will be displayed next to the control. Can be either of the following MsoComboStyle constants: msoComboLabel (the label is displayed) or msoComboNormal (the label isn't displayed).
OnAction Specifies the procedure to be run when the user changes the contents of the combo box control.

The following example adds a combo box to a custom toolbar and assigns the macro named "ScrollToQuarter" to the combo box.

  Set myBar = CommandBars _
    .Add(Name:="Custom", Position:=msoBarTop, _
    Temporary:=True)
myBar.Visible = True 
Set newCombo = myBar.Controls _
    .Add(Type:=msoControlComboBox)
With newCombo
    .AddItem "Q1"
    .AddItem "Q2"
    .AddItem "Q3"
    .AddItem "Q4"
    .Style = msoComboNormal
    .OnAction = "ScrollToQuarter"
End With

While your application is running, the procedure assigned to the OnAction property of the combo box control is called each time the user changes the control. In the procedure, you can use the ActionControl property of the CommandBar object to find out which control was changed and to return the changed value. The ListIndex property will return the item typed or selected in the combo box.

See Also