About Adding New Toolbar Buttons to a Toolbar (VBA/ActiveX)

To add a new toolbar button to a toolbar, use the AddToolbarButton method. This method creates a new ToolbarItem object and adds it to the designated toolbar.

You should only add buttons to a toolbar while the toolbar is visible.

The AddToolbarButton method takes five parameters as input: Index, Name, HelpString, Macro, and FlyoutButton.

Index
The Index parameter is an integer that specifies the position of the new Toolbar item within the toolbar. The index begins with position zero (0) as the first position on the toolbar after the title. To add the new toolbar button to the end of a toolbar, set the Index parameter equal to the Count property of the toolbar. (The Count property of the toolbar represents the total number of toolbar buttons on that toolbar.)

Once a toolbar button has been created, you cannot change the index of the button through the Index property. To change the index of an existing toolbar button, you must delete and re-add the toolbar button to a different position, or add or delete surrounding toolbar buttons until a proper placement is achieved.

Name
A name is a string that identifies the toolbar button. The string must comprise alphanumeric characters with no punctuation other than a dash (-) or an underscore (_). This string is displayed as the tooltip when the cursor is placed over the toolbar button.

Once a toolbar button has been created, you can change the name using the Name parameter.

HelpString
A help string is the text string that appears in the AutoCAD status line when a user highlights a menu item for selection.

Once a toolbar button has been created, you can change the help string for the button using the HelpString parameter.

Macro
A macro is a series of commands that executes specific actions when a toolbar button is selected. Toolbar macros can be simply recordings of keystrokes that accomplish a task, or they can be a complex combination of commands, AutoLISP, DIESEL, or ActiveX programming code.

Once a Toolbar button has been created, you can change the macro for the button using the Macro parameter.

FlyoutButton
The FlyoutButton parameter is an optional flag stating whether or not the new button is to be a flyout button. If the new button is to be a flyout button, this parameter must be set to TRUE. If the new button is not to be a flyout button, this parameter can be set to FALSE or it can be ignored.

Add buttons to a new toolbar

This example creates a new toolbar and adds a button to the toolbar. The button is assigned a macro that will execute the OPEN command when the button is selected.

Sub Ch6_AddButton()
 Dim currMenuGroup As AcadMenuGroup
 Set currMenuGroup = ThisDrawing.Application.MenuGroups.Item(0)

 ' Create the new toolbar
 Dim newToolbar As AcadToolbar
 Set newToolbar = currMenuGroup.Toolbars.Add("TestToolbar")

 ' Add a button to the new toolbar
 Dim newButton As AcadToolbarItem
 Dim openMacro As String

 ' Assign the macro the VB equivalent of "ESC ESC _open "
 openMacro = Chr(3) + Chr(3) + "_open "
 Set newButton = newToolbar.AddToolbarButton _
 ("", "NewButton", "Open a file.", openMacro)
End Sub