Share via


Getting Information About Command Bars and Controls

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Each Microsoft® Office application contains dozens of built-in command bars and can contain as many custom command bars as you choose to add. Each command bar can be one of three types: menu bar, toolbar, or pop-up menu. All of these command bar types can contain additional command bars and any number of controls. To get a good understanding of the command bars object model, it is often best to start by examining the various command bars and controls in an existing application.

You can use the following procedure to print (to the Debug window) information about any command bar and its controls:

Function CBPrintCBarInfo(strCBarName As String) As Variant
   ' This procedure prints (to the Debug window) information
   ' about the command bar specified in the strCBarName argument
   ' and information about each control on that command bar.
   
   Dim cbrBar                     As CommandBar
   Dim ctlCBarControl             As CommandBarControl
   Const ERR_INVALID_CMDBARNAME   As Long = 5
   
   On Error GoTo CBPrintCBarInfo_Err
   
   Set cbrBar = Application.CommandBars(strCBarName)
   
   Debug.Print "CommandBar: " & cbrBar.Name & vbTab & "(" _
      & CBGetCBType(cbrBar) & ")" & vbTab & "(" _
      & IIf(cbrBar.BuiltIn, "Built-in", "Custom") & ")"
   For Each ctlCBarControl In cbrBar.Controls
      Debug.Print vbTab & ctlCBarControl.Caption & vbTab & "(" _
         & CBGetCBCtlType(ctlCBarControl) & ")"
   Next ctlCBarControl

CBPrintCBarInfo_End:
   Exit Function
CBPrintCBarInfo_Err:
   Select Case Err.Number
      Case ERR_INVALID_CMDBARNAME
         CBPrintCBarInfo = "'" & strCBarName & _
            "' is not a valid command bar name!"
      Case Else
         CBPrintCBarInfo = "Error: " & Err.Number _
            & " - " & Err.Description
   End Select
   Resume CBPrintCBarInfo_End
End Function

You call this procedure in the Visual Basic Editor's Immediate window by using the name of a command bar as the only argument. For example, if you execute the following command from the Immediate window:

? CBPrintCBarInfo("Web")

You will see a listing of all the controls and their control types on the Office Web built-in toolbar, as shown in the following figure.

Listing of Web Toolbar Controls

Aa141001.00603(en-us,office.10).gif

When a control type is shown as "Popup," as with the Favorites control above, the control itself is a command bar. You can get a listing of the controls on a pop-up menu command bar by calling the CBPrintCBarInfo procedure and passing in the name of the pop-up menu as the strCBarName argument. For example:

? CBPrintCBarInfo("Favorites")

Note that the CBPrintCBarInfo procedure calls two other custom procedures to get the command bar type and the control type. To get information about every command bar of any type in an application, you can use the PrintAllCBarInfo procedure.

Note   To refer to a member of the CommandBars collection, use the name of the CommandBar object or an index value that represents the object's location in the collection. The controls on a command bar are members of the CommandBar object's Controls collection. To refer to a control in the Controls collection, use the control's Caption property or an index value that represents the control's location within the collection. All collections are indexed beginning with 1.

See Also

Working with Command Bars | Manipulating Command Bars and Command Bar Controls with VBA Code | Creating a Command Bar | Hiding and Showing a Command Bar | Copying a Command Bar | Deleting a Command Bar | Preventing Users from Modifying Custom Command Bars | Working with Personalized Menus | Working with Images on Command Bar Buttons | Working with Command Bar Controls | Working with Command Bar Events