Deleting and Creating the Toolbar

As with a custom menu, you should have a routine to create the toolbar and to delete the toolbar. You always want to make sure that the toolbar is deleted when you close your application.

This method always creates a new toolbar on the fly. Older versions of Excel offered a method for permanently attaching a toolbar to a workbook. Avoid attaching menu bars to the application. It creates numerous headaches when you want to change the toolbar later.

To create the toolbar, add a CommandBar. You have to give the toolbar a name and a position:

Sub DeleteToolbar()

On Error Resume Next CommandBars("XYZ").Delete On Error GoTo 0 End Sub

Sub CreateToolbar()

Dim TBar As CommandBar

Dim NewDD As CommandBarControl

Dim NewBtn as CommandBarButton

1 delete any old copy of the Toolbar DeleteToolbar

1 Define the toolbar Set TBar = CommandBars.Add With TBar

.Name = "XYZ" .Visible = True .Position = msoBarFloating End With

Set NewBtn = TBar.Controls.Add(Type:=msoControlButton) With NewBtn

.OnAction = "PrintCard" .Caption = "Print..." .TooltipText = "Select pages to print" .Style = msoButtonCaption End With End Sub

0 0

Post a comment