Add a new menubar with toggle action : Menu « Application « VBA / Excel / Access / Word






Add a new menubar with toggle action

 
Sub AddNewMB()
   Dim myCommandBar As CommandBar, myCommandBarCtl As CommandBarControl
   Dim myCommandBarSubCtl As CommandBarControl
   
   On Error GoTo AddNewMB_Err
   
   Set myCommandBar = CommandBars.Add(Name:="Sample Menu Bar", Position:=msoBarTop, menuBar:=True, Temporary:=False)
   myCommandBar.Visible = True
   myCommandBar.Protection = msoBarNoMove
   
   Set myCommandBarCtl = myCommandBar.Controls.Add(Type:=msoControlPopup)
   myCommandBarCtl.Caption = "Displa&y"
   
   Set myCommandBarSubCtl = myCommandBarCtl.Controls.Add(Type:=msoControlButton)
   With myCommandBarSubCtl
      .Style = msoButtonIconAndCaption
      .Caption = "E&nable ClickMe"
      .FaceId = 59
      .OnAction = "=ToggleClickMe()"
      .Parameter = 1
      .BeginGroup = True
   End With
   Set myCommandBarSubCtl = myCommandBarCtl.Controls.Add(Type:=msoControlButton)
   
   With myCommandBarSubCtl
      .Style = msoButtonIconAndCaption
      .Caption = "Di&sable ClickMe"
      .FaceId = 276
      .OnAction = "=ToggleClickMe()"
      .Parameter = 2
      .BeginGroup = True
   End With
   
   Set myCommandBarCtl = myCommandBar.Controls.Add(Type:=msoControlButton)
   With myCommandBarCtl
      .BeginGroup = True
      .Caption = "&ClickMe"
      .Style = msoButtonCaption
      .OnAction = "=MsgBox(""You clicked ClickMe"")"
   End With
   
   Set myCommandBarCtl = myCommandBar.Controls.Add(Type:=msoControlButton)
   With myCommandBarCtl
      .BeginGroup = True
      .Caption = "&Set Visibility Off"
      .Style = msoButtonCaption
      .OnAction = "=SampleMenuDisable()"
   End With
   
   Exit Sub

AddNewMB_Err:
   Debug.Print Err.number & vbCr & Err.Description
   Exit Sub
End Sub
Function ToggleClickMe()
   Dim MyMenu As CommandBar
   Dim myCommandBarClickMe As CommandBarControl
   
   On Error GoTo ToggleClickMe_Err
   
   Set MyMenu = CommandBars("Sample Menu Bar")
   Set myCommandBarClickMe = MyMenu.Controls(2)
   
   With CommandBars.ActionControl
      Select Case .Parameter
         Case 1
            myCommandBarClickMe.Enabled = True
         Case 2
            myCommandBarClickMe.Enabled = False
      End Select
   End With
   
   Exit Function

ToggleClickMe_Err:
   Debug.Print Err.number & vbCr & Err.Description
   Exit Function
End Function

Function SampleMenuDisable()
   Application.CommandBars("Sample Menu Bar").Visible = False
   Application.CommandBars("Menu Bar").Visible = True
End Function

 








Related examples in the same category

1.Creating a Menu Bar
2.Controling a CommandBarControl's Visibility
3.Deletes a control tagged "MyMenu2"
4.Create Submenus
5.Adds button, combobox, text box and drop down to tools menu
6.List Menu Information
7.List ShortCut Menus
8.Adding and deleting menus automatically
9.Disabling or hiding menus
10.Working with checked menu items
11.Disabling a shortcut menu
12.Enables the shortcut menu when the workbook is closed.
13.Create a new menu bar that replaces the existing menu bar
14.Adding a menu: Take 1
15.Adding the Budgeting menu to Excel's main menu bar
16.Determined the Id property of the Help menu by executing the following statement:
17.Deleting a menu from a menu bar
18.Adding selections and submenu items to the Budgeting menu
19.Adding a selection to Excel's Tools menu
20.Deleting a menu item from the Tools menu
21.Adding a menu selection that features a shortcut key
22.Adding menu items to shortcut menus
23.Deleting menu items from shortcut menus
24.removes the Hide menu item from two shortcut menus: the one that appears when you right-click a row header and the one that appears for a column header.
25.Disabling shortcut menu items
26.Disabling shortcut menus
27.If you want to disable all shortcut menus, use the following procedure:
28.Resetting shortcut menus: The Reset method restores a shortcut menu to its original condition.
29.Creating an entirely new and separate shortcut menu
30.Adding a new item to the Cell shortcut menu
31.Create a popup menu