Example An Example Toolbar

Sub CreateToolbar()

Dim cbar As CommandBar, cbctl As CommandBarControl

' Delete if it exists

For Each cbar In Application.CommandBars

If cbar.Name = "Toolbar Example" Then cbar.Delete Next

' Create a floating toolbar

Set cbar = Application.CommandBars.Add(Name:="Toolbar Example",

Position:=msoBarFloating) cbar.Visible = True

' Add a custom button control to execute a macro

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

cbctl.Visible = True cbctl.Style = msoButtonCaption cbctl.Caption = "CustomButton"

' Run the following macro cbctl.OnAction = "ExampleMacro"

' Add built-in Open... control

Set cbctl = cbar.Controls.Add(Id:=2 3)

' Icon for button cbctl.Faceld = 23 cbctl.Visible = True

' Add built-in spell checking button Set cbctl = cbar.Controls.Add(Id:=2) cbctl.Faceld = 2 cbctl.Visible = True

Set cbctl = cbar.Controls.Add(Type:=msoControlDropdown)

' Add a tag so macro can find it cbctl.Tag = "ComposerList" cbctl.Visible = True cbctl.Caption = "ListCaption"

' Set list properties of the list box With cbctl

.Addltem "Chopin", 1 .Addltem "Mozart", 2 .Addltem "Bach", 3 .DropDownLines = 0 .DropDownWidth = 7 5

' select nothing to start .Listlndex = 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.

0 0

Post a comment