CommandBars Property Example

The following three procedures set up a new menu item on a toolbar.

Procedure 1:

Sub AddMenuItem()
Dim newMenu As CommandBarControl
Dim toolsMenu As CommandBar
Set toolsMenu = Application.CommandBars("Tools")

Set newMenu = _
  toolsMenu.Controls.Add(msoControlButton, , , , True)

newMenu.Caption = "New &Menu Item"
End Sub

This next procedure connects the click event to the custom button and must be added to a class or form module. The module shown is a form module. This procedure adds a new item to the Tools menu and connects the events of the custom button by assigning the variable e_NewMenu (used in the WithEvents statement) to the custom button variable newMenu.

Procedure 2:

Private Sub AddButton_Click()
Dim newMenu As CommandBarControl
Dim WithEvents e_NewMenu As CommandBarButton

Sub AddMenuItemWithEventHook()
Dim toolsMenu As CommandBar
Set toolsMenu = Application.CommandBars("Tools")

Set newMenu = _
  toolsMenu.Controls.Add(msoControlButton, , , , True)

Set e_NewMenu = newMenu
newMenu.Caption = "New &Menu Item"
End Sub

Private Sub e_NewMenu_Click(ByVal Ctrl As _
  Office.CommandBarButton, CancelDefault As Boolean)
MsgBox "Menu Item Clicked"
ctrl.caption = "Clicked"
End Sub

To execute FrontPage custom menu items using CommandBars collection, index the menu item and call the execute method for that item. The following example inserts a Microsoft Office spreadsheet control at the insertion point.

Procedure 3:

Sub ExecuteMenu()
Dim I As String
Dim C As String
Dim O As String

I = "Insert"
C = "C&omponent"
O = "Office Sp&readsheet"

CommandBars(I).Controls(C).Controls(O).Execute
End Sub

The following example returns the status of various properties of the command bars in the active web.

Private Sub GetCommandBars()
Dim myWeb As Web
Dim myCB As Object
Dim myCBCount As Integer
Dim myDisplayFonts As Boolean
Dim myDisplayKeysInToolTips As Boolean
Dim myLargeButtons As Boolean
Dim myMenuAnimationStyle As String

Set myWeb = ActiveWeb
Set myCB = Application.CommandBars

With myCB
    myCBCount = .Count
    myDisplayFonts = .DisplayFonts
    myDisplayKeysInToolTips = .DisplayKeysInToolTips
    myLargeButtons = .LargeButtons
    myMenuAnimationStyle = .MenuAnimationStyle
End With
End Sub

The following example is a tool that iterates through the command bars and returns several properties from each menu.

Note   To run this example, create a form that has a text box called txtComBar and a command button called cmdComBar, and copy the following code to the code window.

Private Sub cmdComBar_Click()
Dim myWeb As Web
Dim myComBars As Object
Dim myComBar As Object
Dim myText As String
Dim myName As String
Dim myAdaptMenu As String
Dim myEnabledMenu As String
Dim myMenuHeight As String
Dim myMenuWidth As String

Set myWeb = ActiveWeb
Set myComBars = Application.CommandBars
myName = "Name: "
myAdaptMenu = "Menu Adaptive? "
myEnabledMenu = "Menu Enabled? "
myMenuHeight = "Menu Height: "
myMenuWidth = "Menu Width: "
txtComBar.Locked = True
txtComBar.MaxLength = 10000
txtComBar.MultiLine = True
txtComBar.ScrollBars = fmScrollBarsVertical

With myComBars
    For Each myComBar In myComBars
        With myComBar
            myText = myText & myName & .Name & vbCrLf
             myText = myText & myAdaptMenu _
                 & .adaptivemenu & vbCrLf
            myText = _
                myText & myEnabledMenu & .Enabled _
                & vbCrLf
            myText = myText & myMenuHeight & .Height _
                & vbCrLf
            myText = myText & myMenuWidth & .Width _
                & vbCrLf
            txtComBar.Text = myText
        End With
    Next
txtComBar.SetFocus
txtComBar.CurLine = 0
End With
End Sub