Microsoft Office Tutorials and References
In Depth Information
12.6 Example: Creating a Menu
12.6 Example: Creating a Menu
The program shown in Example 12-5 creates the menu system shown in Figure 12-3 o n Excel's
worksheet menu bar. Note that the macros that are invoked by the selection of the menu items are
named ExampleMacro1 and ExampleMacro2 .
Figure 12-3. An example custom menu
Example 12-5. 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&1"
' Action to perform
cbctl.OnAction = "ExampleMacro1"
' 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.
 
 
 
Search JabSto ::




Custom Search