Adding a Single Menu Item

After the MenuObject is defined, you add items to the menu by using MenuObject. Controls.Add to add a command control of the type msoControlButton. The menu item needs to have a Caption that displays on the menu bar, and it has to have an OnAction property. The OnAction property specifies the name of the macro that is executed when the client selects this menu item.

To add several single menu items, repeatedly add new controls to the MenuObject. The following code creates an XYZ menu with four items, as shown in Figure 24.2:

Sub CreateSimpleMenu()

Dim MenuObject As CommandBarPopup Dim Menultem As Object

' Make sure the menus aren't duplicated Call DeleteMenu

Set MenuObject = Application.CommandBars(l) Controls.Add(Type:=msoControlPopup, _ Before:=10, temporary:=True) MenuObject.Caption = "&XYZ Co"

Set Menultem = MenuObject.Controls.Add(Type: Menultem.OnAction = "ImportData" Menultem.Caption = "&Import Data"

msoControlButton)

Set Menultem = MenuObject.Controls.Add(Type: Menultem.OnAction = "ExportData" Menultem.Caption = "&Export Data"

msoControlButton)

Set Menultem = MenuObject.Controls.Add(Type:=msoControlButton) Menultem.OnAction = "ProduceReport" Menultem.Caption = "&Report"

Set Menultem = MenuObject.Controls.Add(Type:=msoControlButton) Menultem.OnAction = "AboutMe" Menultem.Caption = "Abou&t Program" End Sub

Figure 24.2

This simple menu has four items.

Figure 24.2

This simple menu has four items.

0 0

Post a comment