CommandBars Property
Applies To
Application object.
Description
You can use the CommandBars property to return a reference to the CommandBars collection object.
Setting
[application.]CommandBars[.method or property]
The CommandBars property uses the following settings.
Setting | Description |
|
application | Optional. The Application object. |
CommandBars | Required. When used without specifying a method or property, returns the CommandBars collection object. |
method or property | A method or property of the CommandBars collection object. The Item property (collections) is the default property for the CommandBars collection object. |
The CommandBars property is available only by using Visual Basic.
Remarks
The CommandBars collection object is the collection of all built-in and custom command bars in an application. You can refer to individual members of the collection by using the member object's index or a string expression that is the name of the member object. The first member object in the collection has an index value of 1 and the total number of member objects in the collection is the value of the CommandBars collection's Count property.
Once you establish a reference to the CommandBars collection object, you can access all the properties and methods of the object. You can set a reference to the CommandBars collection object by clicking References on the Tools menu while in module Design view. Set a reference to the Microsoft Office 8.0 Object Library in the References dialog box by selecting the appropriate check box.
See Also
CommandBars collection object, Count property ("Microsoft Office Language Reference" in Volume 5), Item property (collections).
Example
The following example uses the CommandBars property of the Application object to manipulate various properties and methods of the CommandBars collection object. To try this example, paste the following code into the Declarations section of a standard module. Create a reference to the Microsoft Office 8.0 Object Library by using the References dialog box, available by clicking References on the Tools menu. Then click anywhere inside the GetCommandBarInfo procedure and press F5 to run it.
Sub GetCommandBarInfo()
Dim strCBBarName As String
strCBBarName = InputBox("Enter the name of a command bar", _
"Get CommandBar Information", "Menu Bar")
If Len(strCBBarName) > 0 Then
CommandBarInfo strCBBarName
End If
End Sub
Sub CommandBarInfo(strCBBarName As String)
Dim cbr As CommandBar
On Error Resume Next
Set cbr = CommandBars(strCBBarName)
If Err = 0 then
With cbr
MsgBox "The command bar named '" & .Name & "' contains " & _
.Controls.Count & IIf(.Controls.Count = 1, " control and ", _
" controls and ") & IIf(.BuiltIn, "is", "is not") & _
" a built-in toolbar", vbOKOnly, "'" & _
.Name & "' Information:"
End With
Else
MsgBox "Invalid CommandBar Name"
End If
End Sub