Microsoft KB Archive/296897

= PPT2000: Sample Visual Basic Code for Creating Toolbars in an Add-In =

Article ID: 296897

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft PowerPoint 2000 Standard Edition

-



This article was previously published under Q296897



SUMMARY
This article provides Microsoft Visual Basic for Applications sample macros that demonstrate how to add and remove CommandBar and CommandBar Buttons objects for use with an add-in.

When you create an add-in, you usually need to expose the procedures and the functions of the add-in to the user. There are several ways to do this:
 * Add CommandBar Buttons (toolbar buttons), as described in this article.
 * Add menu items (not described in this article).
 * Create an event driven add-in that responds to user interaction within the active presentation (not described in this article).

If you create a PowerPoint add-in that exposes its procedures and functions through CommandBar objects or menu items (during the Auto_Open procedure), it is important that you also remove the added CommandBar objects and menu items when your add-in is unloaded (during the Auto_Close procedure).



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The add-in that is discussed in this article is provided as-is. Microsoft does not guarantee that it can be used in all situations. Although Microsoft Support Professionals can help with the installation and existing functionality of this add-in, they will not modify the add-in to provide new functionality.

Sample Code
To create the sample code, follow these steps:  Open Microsoft PowerPoint, and create a new, blank presentation. Press ALT+F11 to open the Microsoft Visual Basic Editor. On the Insert menu, click Module.  Copy the following code, and paste it into the module: ' '' ' AUTO_OPEN '' ' Sub Auto_Open ' In the AutoOpen of the AddIn you want to   ' add your toolbar, and then each button. ' Add the toolbar. AddDeleteToolbars &quot;My Toolbar&quot;, False ' Now add the buttons. AddItemToToolbar &quot;My Toolbar&quot;, &quot;Hello&quot;, &quot;Hello_Toolbar&quot;, 1, 18 ' Some more examples: ' Add a button to the Standard toolbar. 'AddItemToToolbar &quot;Standard&quot;, &quot;X&quot;, &quot;X&quot;, 1, 18 ' Add a button to the VisualBasic toolbar. 'AddItemToToolbar &quot;Visual Basic&quot;, &quot;X&quot;, &quot;X&quot;, 1, 18 ' Add a button to the Formatting toolbar. 'AddItemToToolbar &quot;My Toolbar&quot;, &quot;X&quot;, &quot;X&quot;, 1, 18 End Sub

' '' ' AUTO_CLOSE '' ' Sub Auto_Close ' When closing, it is important to clean up. ' At this point we need to remove all ' command buttons and toolbars we created ' during the AutoOpen process. AddDeleteToolbars &quot;My Toolbar&quot;, True ' Here is an example of deleting buttons from ' different toolbars: ' RemoveItemFromToolbar &quot;Standard&quot;, &quot;X&quot; ' RemoveItemFromToolbar &quot;Visual Basic&quot;, &quot;X&quot; ' RemoveItemFromToolbar &quot;Formatting&quot;, &quot;X&quot;

End Sub

Sub AddDeleteToolbars(strToolbarName As String, _                     Optional bolDelete As Boolean) ' NOTE: We will error if the toolbar already exists. On Error Resume Next Dim cbarApp   As CommandBars Dim cbarMine ' Get the application CommandBar object. Set cbarApp = Application.CommandBars ' If the DELETE switch is OFF... If Not bolDelete Then ' add the toolbar with the name strToolbar... Set cbarMine = CommandBars.Add(Name:=strToolbarName, _                                      Position:=msoBarTop, _                                       Temporary:=True) cbarMine.Visible = True Else ' else we delete the toolbar. cbarApp.Item(strToolbarName).Delete End If End Sub

Sub RemoveItemFromToolbar(strTBName As String, _                         strButtonName As String) Dim cbarMenu   As CommandBars Dim cctlControl As CommandBarControl

' Grab the application CommandBars object. Set cbarToolsMenu = Application.CommandBars ' DELETE: Toolbar button. ' Loop through the CommandBars. For Each cctlControl In cbarToolsMenu(strTBName).Controls With cctlControl ' Check to see if the toolbar is found. If .Caption = strButtonName Then

' If found, remove the command from the menu. .Delete

End If

End With Next cctlControl End Sub

Sub AddItemToToolbar(strTBName As String, _                    strButtonName As String, _                     strMacroName As String, _                     intInsertPosition As Integer, _                     lngFaceID As Long) Dim cbarMenu   As CommandBars Dim cctlControl As CommandBarControl Dim bolFound   As Boolean

' Grab the application CommandBars object. Set cbarMenu = Application.CommandBars

' First, look for button on the toolbar already. For Each cctlControl In cbarMenu(strTBName).Controls With cctlControl ' Look for the toolbar item. If .Caption = strButtonName Then ' Button is found on the toolbar. bolFound = True End If       End With Next cctlControl

' If the button is not found... If Not bolFound And intInsertPosition > 0 Then

' Add the command to the menu. Set cctlControl = cbarMenu(strTBName).Controls.Add _ (Type:=msoControlButton, _                    Before:=intInsertPosition)

' Name the command. ' Set its values. With cctlControl .FaceId = lngFaceID .Caption = strButtonName .OnAction = strMacroName .TooltipText = strButtonName .Enabled = True End With End If End Sub

' '' ' My Macro ' ' This is the macro that gets called when my ' toolbar button gets pressed. '' ' Sub Hello_Toolbar

MsgBox &quot;Hello World&quot; End Sub  On the File menu, click Close and Return to Microsoft PowerPoint. On the File menu, click Save. In the Save as type list, click PowerPoint Add-in (*.ppa).</li> In the File name box, type My Example Add-in, and click OK.</li> On the File menu, click Close. You may want to save the presentation as a PowerPoint presentation file (*.ppt) for future use.

NOTE: If you do not save the presentation, you will not be able to access the code again. Although PowerPoint can create an add-in, it cannot open an add-in after it has been saved.</li> To load the add-in, click Add-Ins on the Tools menu.</li> Click Add New.</li> Browse to the location where you saved you add-in, select the add-in, and then click OK.</li> When the macro warning dialog box appears, click Enable Macros.

In PowerPoint, a new toolbar appears that contains a single button. If you click the button on the new toolbar, you receive the following message:

Hello World

</li> To unload the add-in, click Add-ins on the Tools menu. In the list of add-ins, click My Example Add-in, and then click Unload.

NOTE: Because of the Auto-Close macro, the toolbar that the add-in created has now been removed.</li></ol>

<div class="references_section">