CommandBars Property

Microsoft FrontPage Visual Basic

Returns a CommandBars collection that represents the menu and toolbars displayed in Microsoft FrontPage.

expression.CommandBars

expression    Required. An expression that returns an Application object.

Remarks

You can't access FrontPage-specific pop-up shortcut menus using the CommandBars collection. For example, you can't access the shortcut menu that appears when you right-click a page in Page view. However, you can create your own pop-up shortcut menus using the ShowPopup method for the CommandBar object.

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

The following 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 the 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 site.

Private Sub GetCommandBars()
    Dim myWeb As WebEx
    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 WebEx
    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