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.
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
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.
Note
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.
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.
In the Add New Item dialog box, select Ribbon support. Click Add to insert the new Office Fluent Ribbon customization.
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.
Save and close Ribbon1.xml.
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
In the Solution Explorer window, right-click the SetPrintPropertiesAddIn project, and select Properties from the context menu.
In the Properties pane, select the Resources tab.
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.
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
To add a using statement
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.
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.
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.
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.
Note
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.
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.
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. |
Watch the Video
Video Length: 00:09:23
File Size: 08.57 MB WMV
|