Export (0) Print
Expand All

Using Visual Studio 2005 Tools for the Office System SE to Create PowerPoint Add-Ins

Office 2007

Office Visual How To

Applies to:2007 Microsoft Office System, Microsoft Office PowerPoint 2007, Visual Studio 2005, Visual Studio 2005 Tools for Office Second Edition

Ken Getz, MCW Technologies, LLC

May 2007

Overview

Microsoft Visual Studio 2005 Tools for Office Second Edition (VSTO 2005 SE) makes it easy to create application-level add-ins for Office 2007 products, including Microsoft Office PowerPoint 2007. You can use application-level add-ins to extend the built-in functionality in the host application, or to add support for domain-specific functionality. In this article, you learn to create a simple add-in that prints the current presentation using pre-configured settings instead of configuring the settings each time you print. If you often print a presentation in pure black and white, two slides per page, you might appreciate the simplicity and convenience of creating and using this add-in.

Code It

In this demonstration, you customize the Office Fluent Ribbon by adding a button to the Add-Ins tab in Office PowerPoint 2007. Clicking the button prints the current presentation with two slides per page, in pure black and white format.

To create the Office Fluent Ribbon customization

  1. In Visual Studio 2005, create a new project. After you select the language (Visual Basic or C#), expand the Office node, and select Office 2007 Add-Ins. In the Templates pane, select PowerPoint Add-In. Name the new project SetPrintPropertiesAddIn.

    NoteNote
    Visual Studio creates two projects in the solution. The first project contains your add-in, and the second project contains a Setup project that you can use when distributing your add-in.
  2. This add-in requires an Office Fluent Ribbon customization. To add the customization, in the Solution Explorer window, right-click the add-in project. From the context menu, select Add, and then New Item.

  3. In the Add New Item dialog box, select Ribbon support. Click Add to insert the new Office Fluent Ribbon customization.

  4. Modify the new Ribbon1.xml file, replacing the existing content with the XML code that follows this procedure.

    This customization adds a new tab to the Office Fluent Ribbon, displaying the text Common Tasks. On this tab, the markup adds a new button displaying the text Print 2.

  5. Save and close Ribbon1.xml.

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad">
  <ribbon>
    <tabs>
      <tab id="CommonTasksAddIns" label="Common Tasks">
        <group id="Group1"
        label="My Stuff" visible="true">
          <button id="SetPrint"
            label="Print 2"
            size="large"
            screentip ="Print two framed slides per page"
            onAction="PrintButtonClick"
            imageMso="FilePrintQuick"/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

The add-in must load the Ribbon1.xml file at runtime. By default, the template includes a procedure that handles this in a standard way. It's far easier to load the Ribbon1.xml file as a resource.

To load the Ribbon1.xml file as a resource

  1. In the Solution Explorer window, right-click the SetPrintPropertiesAddIn project, and select Properties from the context menu.

  2. In the Properties pane, select the Resources tab.

  3. From the Solution Explorer window, drag the Ribbon1.xml file into the Properties pane. This action adds the Ribbon1.xml file as a project resource.

  4. Close the Properties pane, and click Yes when prompted to save.

At runtime, the add-in calls a special procedure to load a copy of your Office Fluent Ribbon customization, and you must add that procedure. The add-in template includes the procedure—you must simply uncomment it.

To add the ThisAddIn procedure

  • In the Ribbon1.vb or Ribbon1.cs file, uncomment the ThisAddIn partial class.

To add a using statement

  • In C# only, at the top of the code file, add the following using statement.

using PowerPoint=Microsoft.Office.Interop.PowerPoint;

To modify GetCustomUI

  • In the Ribbon1 class, modify the existing GetCustomUI implementation. In C#, you must expand the IRibbonExtensibility members code region to find the procedure so that it retrieves the Ribbon1 resource.

return Properties.Resources.Ribbon1;

To add a callback procedure

  • When you customize the Office Fluent Ribbon, you must add callback procedures to handle events of the Office Fluent Ribbon controls. In this case, you must react to the Click event of the new button. In the Ribbon1 class, add the following callback procedure so that clicking the new button on the Office Fluent Ribbon calls this procedure.

public void PrintButtonClick(Office.IRibbonControl control)
{
}

To verify the new buttons and replace PrintButtonClick

  1. Save the project, and run it. In PowerPoint 2007, on the Ribbon, click the new Common Tasks tab, and verify that you see the new button on the Office Fluent Ribbon. Close PowerPoint when you are done, returning to Visual Studio 2005.

  2. Replace the existing PrintButtonClick procedure in the Ribbon1 class with the following procedure.

    The code retrieves a reference to the active presentation, and then retrieves a reference to the print options for the presentation. The code sets the output type, turns on the option to frame the slides, sets the print color type to pure black and white, enables background printing, and calls the PrintOut method, printing the entire presentation.

    NoteNote
    This code uses the Globals.ThisAddIn.Application reference. The Globals class is created by the add-in template. The class provides access to the add-in from anywhere in the project, and therefore, provides access to the host Application object.
public void PrintButtonClick(Office.IRibbonControl control)
{
  // Retrieve a reference to the active presentation:
  PowerPoint.Presentation presentation = 
   Globals.ThisAddIn.Application.ActivePresentation;

  // Retrieve a reference to the PowerPoint printing options:
  PowerPoint.PrintOptions options = 
   presentation.PrintOptions;

  // Set the various printing options:
  options.OutputType = 
   PowerPoint.PpPrintOutputType.ppPrintOutputTwoSlideHandouts;
  options.FrameSlides = 
   Microsoft.Office.Core.MsoTriState.msoCTrue;
  options.PrintColorType = 
   PowerPoint.PpPrintColorType.ppPrintPureBlackAndWhite;
  options.PrintInBackground = 
   Microsoft.Office.Core.MsoTriState.msoTrue;

  // Print the presentation:
  presentation.PrintOut( 
   1,presentation.Slides.Count,String.Empty,
   1,Microsoft.Office.Core.MsoTriState.msoFalse);
}

Save and run the project. In the running instance of PowerPoint 2007, create a few slides, and add some text to the slides. Select the Common Tasks tab, and click the Print 2 button. Verify that the presentation prints to the default printer, two slides per page, in pure black and white.

Close PowerPoint when you are done, returning to Visual Studio.

Read It

The PowerPoint object model is richer than this one simple add-in demonstrates. For more information about the PowerPoint object model in code, see the PowerPoint Help documentation and the documentation listed in the Explore It section.

See It

Using VSTO to Create Add-Ins w/ Custom Task Panes

Watch the Video

Video Length: 00:09:23

File Size: 08.57 MB WMV

Explore It
Show:
© 2014 Microsoft