Microsoft KB Archive/296897

From BetaArchive Wiki
Knowledge Base


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:

  1. Open Microsoft PowerPoint, and create a new, blank presentation.
  2. Press ALT+F11 to open the Microsoft Visual Basic Editor.
  3. On the Insert menu, click Module.
  4. 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 "My Toolbar", False
        ' Now add the buttons.
        AddItemToToolbar "My Toolbar", "Hello", "Hello_Toolbar", 1, 18
        
        ' Some more examples:
        
        ' Add a button to the Standard toolbar.
        'AddItemToToolbar "Standard", "X", "X", 1, 18
        ' Add a button to the VisualBasic toolbar.
        'AddItemToToolbar "Visual Basic", "X", "X", 1, 18
        ' Add a button to the Formatting toolbar.
        'AddItemToToolbar "My Toolbar", "X", "X", 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 "My Toolbar", True
        ' Here is an example of deleting buttons from
        ' different toolbars:
        ' RemoveItemFromToolbar "Standard", "X"
        ' RemoveItemFromToolbar "Visual Basic", "X"
        ' RemoveItemFromToolbar "Formatting", "X"
    
    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 "Hello World"
        
    End Sub
                        
  5. On the File menu, click Close and Return to Microsoft PowerPoint.
  6. On the File menu, click Save.
  7. In the Save as type list, click PowerPoint Add-in (*.ppa).
  8. In the File name box, type My Example Add-in, and click OK.
  9. 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.
  10. To load the add-in, click Add-Ins on the Tools menu.
  11. Click Add New.
  12. Browse to the location where you saved you add-in, select the add-in, and then click OK.
  13. 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

  14. 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.


REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

222685 PPT2000: How to Auto Load a PowerPoint Add-In


222737 PPT2000: How to Create a PowerPoint Add-In


234184 PPT2000: How to Create Application Level Event Handlers



Additional query words: ppt200 ppt powerpnt 2000 9.0 ppt9 addin add-ins adding macros macro level toolbars menuitems menu items tool bar command buttons example sample reference free code

Keywords: kbhowto kbsample KB296897