Microsoft Office Tutorials and References
In Depth Information
Example 12-7. The ExampleMacro Macro
' Set list properties of the list box
With cbctl
.AddItem "Chopin", 1
.AddItem "Mozart", 2
.AddItem "Bach", 3
.DropDownLines = 0
.DropDownWidth = 75
' select nothing to start
.ListIndex = 0
End With
' Set macro to execute when an item
' is selected
cbctl.OnAction = "ExampleListMacro"
End Sub
The first step is to check for an existing toolbar named Toolbar Example. If it exists, we delete it.
Then we create a floating toolbar named Toolbar Example. The name is important, since we will
use it later for identification.
Next, we add a custom button control ( Id argument missing) and assign it the macro
ExampleMacro , whose code, which is shown in Example 12-7 , simply tells us that we pushed
the button.
Example 12-7. The ExampleMacro Macro
Sub ExampleMacro()
MsgBox "Custom button pressed"
End Sub
Next, we add a built-in File Open... custom control, whose Id happens to be 23. (We have already
discussed how to get built-in control IDs.) This custom control automatically displays the Open
dialog box. Note that we set the FaceId to 23 as well. This displays the default icon for the Open
command, but we could choose another icon if desired.
Then we add the built-in Spelling custom control, which checks the spelling of the active
Finally, we add a custom list box and populate it with the names of three composers. Note that we
set the Tag property of this list box. The reason is that we want to be able to use the FindControl
method to find the list box from within the macro that is assigned to the OnAction property, which
is shown in Example 12-8 .
Example 12-8. Macro Invoked by Selecting a Composer from the List Box
Sub ExampleListMacro()
Dim cbctl As CommandBarControl
Find the list box control
Set cbctl = CommandBars("Toolbar Example"). _
If Not cbctl Is Nothing Then
MsgBox "You selected " & cbctl.List(cbctl.ListIndex)
End If
End Sub
Search JabSto ::

Custom Search