Microsoft KB Archive/163461

From BetaArchive Wiki

Article ID: 163461

Article Last Modified on 10/11/2006



APPLIES TO

  • Microsoft PowerPoint 98 for Macintosh
  • Microsoft PowerPoint 97 Standard Edition



This article was previously published under Q163461

SUMMARY

This article describes how to create an add-in for Microsoft PowerPoint using Microsoft Visual Basic for Applications. The sample macro (Sub procedure) adds a command to the Tools menu to allow you to change your view to slide sorter view if you are not already in slide sorter view.

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.

Step 1 - Create the Code for the Add-In

  1. Start PowerPoint and create a blank presentation.
  2. On the Tools menu, point to Macro, and then click Visual Basic Editor.
  3. In the Visual Basic Editor, click Module on the Insert menu.
  4. Type the following code in the Module window.

    NOTE: You can substitute your code for the following code.

          Sub ChangeView()
    
             ' Check to see whether a presentation is open.
             If Presentations.Count <> 0 Then
                If ActiveWindow.ViewType <> ppViewSlideSorter Then
                   ActiveWindow.ViewType = ppViewSlideSorter
                End If
             Else
                MsgBox "No presentation open. Open a presentation and " _
                   & "run the macro again.", vbExclamation
             End If
          End Sub

    NOTE: A line that is preceded by an apostrophe (') introduces a comment in the code. Comments are provided to explain what the code is doing at a particular point in the procedure. This text is optional and may be excluded from your code.

Step 2 - Create the Auto_Open Macro

The Auto_Open macro stores initialization code for your add-in and it is automatically executed when the add-in is loaded by PowerPoint. The following code, adds a command (Change to Slide Sorter) to the Tools menu that executes your add-in code.

     Sub Auto_Open()

      Dim NewControl As CommandBarControl

      ' Store an object reference to a command bar.
      Dim ToolsMenu As CommandBars

      ' Figure out where to place the menu choice.
      Set ToolsMenu = Application.CommandBars

      ' Create the menu choice. The choice is created in the first
      ' position in the Tools menu.
      Set NewControl = ToolsMenu("Tools").Controls.Add _
                       (Type:=msoControlButton, _
                        Before:=1)

      ' Name the command.
      NewControl.Caption = "Change to Slide Sorter"

      ' Connect the menu choice to your macro. The OnAction property
      ' should be set to the name of your macro.
      NewControl.OnAction = "ChangeView"

   End Sub
                

Step 3 - Create the Auto_Close Macro

The Auto_Close macro is executed when an add-in is unloaded by PowerPoint. The Auto_Close macro stores your clean-up code. The following code removes the command you added to the Tools menu in the "Create the Auto_Open Macro" section of this article.

   Sub Auto_Close()

      Dim oControl As CommandBarControl
      Dim ToolsMenu As CommandBars

      ' Get an object reference to a command bar.
      Set ToolsMenu = Application.CommandBars

      ' Loop through the commands on the tools menu.
      For Each oControl In ToolsMenu("Tools").Controls

            ' Check to see whether the comand exists.
            If oControl.Caption = "Change to Slide Sorter" Then

              ' Check to see whether action setting is set to ChangeView.
              If oControl.OnAction = "ChangeView" Then

                  ' Remove the command from the menu.
                  oControl.Delete
               End If
            End If
      Next oControl
   End Sub

                

Step 4 - Create the .ppa File

  1. On the Debug menu, click Compile VBAProject.

    If your code has compile errors, you will not be able to create the add-in.

    NOTE: This step is only required if the Auto Syntax Check option in the Visual Basic Editor is not selected. To turn this option on or off, use these steps:

    1. Click Options on the Tools menu, and then click the Editor tab.
    2. Clear or select the Auto Syntax Check check box, and then click OK.

      If this option is currently selected, skip to step 2.
    3. Save the presentation.
    4. On the File menu, click Close and Return to Microsoft PowerPoint.
    5. On the File menu, click Save As.
    6. Select Presentation (*.ppt) in the Save As Type list.
    7. Name the presentation, and then click Save.

      Your add-in code is now saved in the presentation.
    8. Create the .ppa file.
    9. On the File menu, click Save As.
    10. Select PowerPoint Add-In (*.ppa) in the Save As Type list.
    11. In the File Name box, type a name for your add-in, and then click Save.

      Typically, PowerPoint add-ins are placed in the c:\Program Files\ Microsoft Office\Office folder. However, you can choose another folder if you want.

Step 5 - Load the Add-In

  1. On the Tools menu, click Add-Ins.
  2. Click Add New.
  3. In the Add New PowerPoint Add-In dialog box, select the add-in file you created in the "Step 4 - Create the PPA File" section. Click OK.
  4. In the macro warning message box, click Enable Macros.


NOTE: If you received an add-in file from an unknown source, you should click Disable Macros.

The Auto_Open macro now executes.

For information on how to use the PowerPoint object model to load add-ins, click the Office Assistant, type "Loaded property," click Search, and then click to view "Loaded Property."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Visual Basic Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base: How to Add/Remove a Single Office Program or Component

Step 6 - Unload the Add-In

Use the following steps to unload an add-in:

  1. On the Tools menu, click Add-Ins.
  2. From the Available Add-Ins list, select the add-in you want to unload, and then click Unload or Remove.


When you click Unload, the add-in is unloaded but it remains on the Available Add-Ins list. When you click Remove, the add-in is unloaded and it is removed from the Available Add-Ins list.

The Auto_Close macro now executes.

For information on how to use the PowerPoint object model to load add-ins, click the Office Assistant, type "Loaded property," click Search, and then click to view "Loaded Property."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Visual Basic Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

120802 How to Add/Remove a Single Office Program or Component


Step 7 - Protecting Your Add-In with a Password

When you save a presentation as an add-in, PowerPoint does not protect your source code. You can protect your code with a password, using the following steps.

NOTE: You must protect your project with a password before you save the add-in.

  1. On the Tools menu, click VBAProject Properties, and then click the Protection tab.
  2. Click to select the Lock Project For Viewing check box.
  3. Under Password To View Project Properties, type the password you want in the Password and Confirm Password boxes.
  4. Click OK.


A password is now required to view your source code.


REFERENCES

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

163435 Programming Resources for Visual Basic for Applications



Additional query words: 8.00 ppt8 vba vbe addin addins macppt mac_ppt ppt98 powerpt

Keywords: kbcode kbdtacode kbhowto kbmacro kbprogramming KB163461