any power users build presentations using data from Excel or other data sources. This article shows how to automate Microsoft PowerPoint 2003 from within a Visual Studio 2005 application. The article presents a class called GenPPT, which creates several different types of slides, including slides that integrate tables and charts. GenPPT is written in Visual Basic 2005, and the demo program that calls it is written in C#: this demonstrates using multiple .NET languages in a solution.
Several times in the last year, I've seen online questions about generating PowerPoint output automatically. Just like Microsoft Word and Microsoft Excel, PowerPoint contains a rich object model that a developer can utilize to generate attractive and appealing slideshows. So I decided to devote a Baker's Dozen
article to the topic of PowerPoint automation within Visual Studio 2005.
If you're not sure how to perform a specific programming task in PowerPoint (or any Microsoft Office Product), create a macro and then perform the task. In most instances, the code in the VBA macro will show you the way.
This article presents a Visual Basic 2005 class that aids a developer with the following PowerPoint tasks:
- General structure of GenPPT
- Creating an instance of PowerPoint
- Starting a new presentation with a template and a title page
- Creating a text-only slide with a title and a set of bullet points
- Baker's Dozen Spotlight: building an Excel table that you can use to generate a PowerPoint table or a Chart object
- Creating a slide that displays an Excel table
- Building an Excel table that you can use as a source for an Excel chart object
- Customizing the display of an Excel chart object
- Creating a slide that displays both a table and a pie chart
- Creating a slide that displays a line chart
- Setting animations and slide transitions
- Defining page footer information
- Saving and displaying the presentation
Deciding on an example to illustrate a set of classes can be an interesting thought process. Initially I was going to use data from the Northwind databasebut since it's near the end of the football season (I'm writing this in mid-December), I decided to have some fun with it. I am a big Kansas City Chiefs fan, and it was only a few years ago that KC started the season 9-0 and had Super Bowl hopes. Of course, it didn't turn out that way, but I decided to build a PowerPoint presentation on their 2003 season. Here we go!
Tip 1: General Information for GenPPT
GenPPT is a DLL written in Visual Basic 2005. It contains COM references to the Microsoft Excel 11.0 and Microsoft PowerPoint 11.0 Object Libraries. It also contains COM references to Microsoft Core Office Library and Visual Basic for Application Extensibility: Visual Studio 2005 automatically adds these references when you add the PowerPoint and Excel object libraries.
To add these libraries manually, bring up the Solution Explorer window (from the View menu choose Solution Explorer), navigate to the References folder, right-click and choose "Add Reference." From the Add Reference window, click the COM tab and scroll down to the Microsoft Excel and Microsoft PowerPoint Object Libraries to select them.
GenPPT contains one class file, PowerPointTools.vb
. The class contains the following import statements to access the PowerPoint and Excel object models.
Imports PowerPoint = _
Imports Graph = Microsoft.Office.Interop.Graph
Imports Excel = Microsoft.Office.Interop.Excel
Table 1 and Table 2 list all the public properties and methods of GenPPT. GenPPT also contains a few support methods for common tasks.
Table 1: GenPPT public properties.
Table 2: Primary GenPPT public methods.
||Object reference to the PowerPoint application
||Object reference to the Excel application
||Current slide number
||Object reference to the current Excel worksheet
||Creates an instance of Powerpoint and Excel
||Defines the default template to be used
||Defines the footer of the PPT
||Builds the PPT title page slide
||Builds a slide of bullet points, using a DataTable source
||Builds a slide with a table, using a DataTable source
||Builds a slide with a table and chart, using DataTable sources
||Builds a slide with a chart, using a DataTable source
||Saves the generated PPT
||Displays the generated PPT
||Add a picture to be centered on the current slide
||Build a footer
||Sets the slide-to-slide transition effect