Adding and Deleting Toolbar Buttons

If you want a specific toolbar button to appear on a toolbar only under certain conditions, you can add and delete the button at run time. When you add a toolbar button, you can attach a macro to it or change the ToolTip text associated with it.

The Add method for the ToolbarButtons collection adds a new toolbar button to an existing built-in or custom toolbar. The onAction argument of the Add method specifies the procedure that will run when the user clicks the toolbar button.

Use the Name property for the new toolbar buttons to set the ToolTip text (the text that appears beneath a toolbar button when the mouse pointer is directly over that button). To set the status bar text (the text that appears appears in the status bar when the mouse pointer is directly over a button), use the statusBar argument of the Add method.

The following example adds two toolbar buttons, separated by a gap, to the toolbar "MyAppTools."


With Toolbars("MyAppTools").ToolbarButtons

    ' Add a button with the clock face
    Set newButton1 = .Add( _
        Button:=213, _
        before:=1, _
        OnAction:="Module1.MyScheduler", _
        Enabled:=True, _
        Pushed:=False, _
        StatusBar:="Run custom scheduler")    ' sets status bar text
    newButton1.Name = "Scheduler"            ' sets tool tip text

    ' Add a button with the scissors face
    Set newButton2 = .Add( _
        Button:=12, _
        before:=2, _
        OnAction:="Module1.MyCutProc", _
        Enabled:=True, _
        Pushed:=False, _
        StatusBar:="Custom cut command")        ' sets status bar text
    newButton2.Name = "Custom Cut"            ' sets tool tip text

    ' Add a space between the buttons
    .Add Button:=0, before:=2
End With

If you don't specify a position for a new toolbar button, the button is added at the end of the toolbar. If you don't specify a procedure to run when the toolbar button is clicked and it's a built-in button, the default action occurs. If you don't specify a procedure for a custom toolbar button, nothing happens when the button is clicked.

The Delete method deletes a toolbar button. The following example deletes toolbar button three from the Standard toolbar.


Toolbars("Standard").ToolbarButtons(3).Delete