Example An Example Menu

Sub CreatePopup()

Dim cbpop As CommandBarControl Dim cbctl As CommandBarControl Dim cbsub As CommandBarControl

' Create a popup control on the main menu bar

Set cbpop = Application.CommandBars("Worksheet Menu Bar").

Controls.Add(Type:=msoControlPopup) cbpop.Caption = "&Custom" cbpop.Visible = True

' Add a menu item

Set cbctl = cbpop.Controls.Add(Type:=msoControlButton) cbctl.Visible = True

' Next is required for caption cbctl.Style = msoButtonCaption cbctl.Caption = "MenuItem&l"

' Action to perform cbctl.OnAction = "ExampleMacrol"

' Add a popup for a submenu

Set cbsub = cbpop.Controls.Add(Type:=msoControlPopup)

cbsub.Visible = True cbsub.Caption = "&SubMenuItem1"

' Add a menu item to the submenu

Set cbctl = cbsub.Controls.Add(Type:=msoControlButton)

cbctl.Visible = True

' Next is required for caption cbctl.Style = msoButtonCaption cbctl.Caption = "SubMenuItem&2"

' Action to perform cbctl.OnAction = "ExampleMacro2" End Sub

Note also the use of the ampersand character (&) in the Caption properties. This character signals a hot key (or accelerator key). Thus, "&Custom" appears as Custom in the menu bar and can be invoked using the keystroke combination Alt-C.

0 0

Post a comment