Udostępnij za pośrednictwem


Walkthrough: Creating Your First Application-Level Add-in for PowerPoint

This walkthrough shows you how to create an application-level add-in for Microsoft Office PowerPoint. The features that you create in this kind of solution are available to the application itself, regardless of which presentations are open. For more information, see Office Solutions Development Overview.

Applies to: The information in this topic applies to application-level projects for PowerPoint 2007 and PowerPoint 2010. For more information, see Features Available by Office Application and Project Type.

This walkthrough illustrates the following tasks:

  • Creating a PowerPoint add-in project for PowerPoint.

  • Writing code that uses the object model of PowerPoint to add a text box to each new slide.

  • Building and running the project to test it.

  • Cleaning up the project so that the add-in no longer runs automatically on your development computer.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

link to video For a related video demonstration, see How Do I: Create an Add-in for Microsoft PowerPoint?.

Prerequisites

You need the following components to complete this walkthrough:

-

An edition of Visual Studio 2010 that includes the Microsoft Office developer tools. For more information, see [Configuring a Computer to Develop Office Solutions](bb398242\(v=vs.100\).md).
  • PowerPoint 2007 or PowerPoint 2010.

Creating the Project

To create a new project

  1. Start Visual Studio.

  2. On the File menu, point to New, and then click Project.

  3. In the templates pane, expand Visual C# or Visual Basic, and then expand Office.

  4. Under the expanded Office node, select the 2007 if you have PowerPoint 2007 installed, or select the 2010 node if you have PowerPoint 2010 installed.

  5. In the list of project templates, select PowerPoint 2007 Add-in or PowerPoint 2010 Add-In.

  6. In the Name box, type FirstPowerPointAddIn.

  7. Click OK.

    Visual Studio creates the FirstPowerPointAddIn project and opens the ThisAddIn code file in the editor.

Writing Code that Adds Text to Each New Slide

Next, add code to the ThisAddIn code file. The new code uses the object model of PowerPoint to add a text box to each new slide. By default, the ThisAddIn code file contains the following generated code:

  • A partial definition of the ThisAddIn class. This class provides an entry point for your code and provides access to the object model of PowerPoint. For more information, see Programming Application-Level Add-Ins. The remainder of the ThisAddIn class is defined in a hidden code file that you should not modify.

  • The ThisAddIn_Startup and ThisAddIn_Shutdown event handlers. These event handlers are called when PowerPoint loads and unloads your add-in. Use these event handlers to initialize your add-in when it is loaded, and to clean up resources used by your add-in when it is unloaded. For more information, see Events in Office Projects.

To add a text box to each new slide

  1. In the ThisAddIn code file, add the following code to the ThisAddIn class. This code defines an event handler for the PresentationNewSlide event of the Application object.

    When the user adds a new slide to the active presentation, this event handler adds a text box to the top of the new slide, and it adds some text to the text box.

    Private Sub Application_PresentationNewSlide(ByVal Sld As PowerPoint.Slide) _
        Handles Application.PresentationNewSlide
        Dim textBox As PowerPoint.Shape = Sld.Shapes.AddTextbox( _
            Office.MsoTextOrientation.msoTextOrientationHorizontal, 0, 0, 500, 50)
        textBox.TextFrame.TextRange.InsertAfter("This text was added by using code.")
    End Sub
    
    void Application_PresentationNewSlide(PowerPoint.Slide Sld)
    {
        PowerPoint.Shape textBox = Sld.Shapes.AddTextbox(
            Office.MsoTextOrientation.msoTextOrientationHorizontal, 0, 0, 500, 50);
        textBox.TextFrame.TextRange.InsertAfter("This text was added by using code.");
    }
    
  2. If you are using C#, add the following code to the ThisAddIn_Startup event handler. This code is required to connect the Application_PresentationNewSlide event handler with the PresentationNewSlide event.

    this.Application.PresentationNewSlide += 
        new PowerPoint.EApplication_PresentationNewSlideEventHandler(
        Application_PresentationNewSlide);
    

To modify each new slide, the previous code examples use the following objects:

  • The Application field of the ThisAddIn class. The Application field returns an Application object, which represents the current instance of PowerPoint.

  • The Sld parameter of the event handler for the PresentationNewSlide event. The Sld parameter is a Slide object, which represents the new slide. For more information, see PowerPoint Solutions.

Testing the Project

When you build and run the project, verify that the text box appears in new slides that you add to a presentation.

To test the project

  1. Press F5 to build and run your project.

    When you build the project, the code is compiled into an assembly that is put in the build output folder for the project. Visual Studio also creates a set of registry entries that enable PowerPoint to discover and load the add-in, and it configures the security settings on the development computer to enable the add-in to run. For more information, see Office Solution Build Process Overview.

  2. In PowerPoint, add a new slide to the active presentation.

  3. Verify that the following text is added to a new text box at the top of the slide.

    This text was added by using code.

  4. Close PowerPoint.

Cleaning up the Project

When you finish developing a project, remove the add-in assembly, registry entries, and security settings from your development computer. Otherwise, the add-in will run every time you open PowerPoint on the development computer.

To clean up your project

  • In Visual Studio, on the Build menu, click Clean Solution.

Next Steps

Now that you have created a basic application-level add-in for PowerPoint, you can learn more about how to develop add-ins from these topics:

See Also

Concepts

PowerPoint Solutions

Other Resources

Programming Application-Level Add-Ins

Office UI Customization

Building and Debugging Office Solutions

Deploying Office Solutions

Office Project Templates Overview