Adding a menu item

The example in the preceding section demonstrates how to create a new menu. The following example adds a menu item to the Excel Format menu. This menu item, when clicked, executes a macro named ToggleWordWrap. The ToggleWordWrap procedure changes the WrapText property of the selected cells.

After creating the menu item, I change the Caption, OnAction, and BeginGroup properties. Setting BeginGroup to True displays a separator bar before the new menu item. Figure 20-5 shows the modified Format menu.

Sub AddMenuItem()

Set Item = CommandBars(1).Controls("Format")

.Controls.Add Item.Caption = "&Toggle Word Wrap" Item.OnAction = "ToggleWordWrap" Item.BeginGroup = True End Sub

Figure 20-5:

The Format menu has a new menu item: Toggle Word Wrap.

Figure 20-5:

The Format menu has a new menu item: Toggle Word Wrap.

The ToggleWordWrap procedure, available on this book's Web site, is shown next:

Sub

ToggleWordWrap()

If TypeName(Selection) =

"Range" Then

Selection.WrapText =

Not ActiveCell.WrapText

End If

End

Sub

0 0

Post a comment