Retrieving the Number of Slides from PowerPoint 2010 Presentations by Using the Open XML SDK 2.0
Summary: Use the strongly-typed classes in the Open XML SDK 2.0 for Microsoft Office to retrieve the number of slides in a Microsoft PowerPoint 2010 presentation, without loading the presentation into Microsoft PowerPoint.
Applies to: Excel 2010 | Office 2007 | Office 2010 | Open XML | PowerPoint 2007 | PowerPoint 2010 | VBA | Word 2010
Published: July 2011
Provided by: Ken Getz, MCW Technologies, LLC
The Office Open XML file formats make it possible to retrieve information about PowerPoint presentations. The Open XML SDK 2.0 for Microsoft Office adds strongly-typed classes that simplify access to the Office Open XML file formats. The SDK is designed to simplify the tasks of working with, for example, information about slides in a presentation. The code sample that is included with this Visual How To describes how to the use the SDK to retrieve the number of slides in a presentation (either including hidden slides or not), without requiring you to open the presentation in Microsoft PowerPoint. The code sample provided with this Visual How To includes the code that you need to retrieve the count of slides in a PowerPoint 2007 or PowerPoint 2010 presentation. The following sections show you the code, in detail. Setting Up References To use the code from the Open XML SDK 2.0, add the following references to your project. The sample project already includes these references, but in your code, you would have to explicitly reference the following assemblies:
Also ensure that your code includes at least the following using/Imports statements at the top of the code file:
Examining the Procedure The PPTGetSlideCount procedure accepts two parameters: a string indicating the path of the file to examine and an optional Boolean value that indicates whether to include hidden slides in the count. The method returns an integer that indicates the number of slides (counting either all the slides, or only visible slides, depending on the second parameter value).
The procedure works with the presentation that you specify, returning the count of slides in the presentation. To call the procedure, pass the required parameter value (and optionally, a Boolean value indicating whether to include hidden slides in the count), as shown in the code example. To demonstrate the procedure, create a presentation, and pass the path for the sample file to the PPTGetSlideCount procedure.
Accessing the Presentation The code starts by creating an integer variable that can contain the return value, the number of slides that is named slidesCount. The code then opens the specified presentation by using the Open(String, Boolean) method and indicating that the document should be open for read-only access (the final false parameter). Given the open presentation, the code uses the PresentationPart property to navigate to the main presentation part, storing the reference in a variable named presentationPart.
Retrieving a Count of All Sheets If the presentation part reference is not null (and it will not be, for any presentation that loads correctly into PowerPoint), the code next calls the SlideParts.Count method of the presentation part. If you requested all slides that included hidden slides, that is all there is to do. However, there is slightly more work to be done if you want to exclude hidden slides.
Retrieving a Count of Visible Sheets If you requested that the code should limit the return value to include only visible slides, the code must filter its collection of slides to include only those slides that have a Show property that contains a value, and is True. If the Show property is null, that also indicates that the slide is visible. This is the most likely scenario. PowerPoint does not set the value of this property, in general, unless the slide is to be hidden. The only way the Show property would exist and have a value of True would be if you had hidden and then unhidden the slide. The sample code uses the Where function together with a lambda expression to do the work.
Sample Procedure
The code sample included with this Visual How To describes code that retrieves the count of slides in a PowerPoint presentation. To use the sample, install the Open XML SDK 2.0, available from the link listed in the Explore It section. The sample also uses a modified version of code included as part of a set of code examples for the Open XML SDK 2.0. The Explore It section also includes a link to the full set of examples, although you can use the sample without downloading and installing the code examples. The sample application demonstrates only several the available properties and methods provided by the Open XML SDK 2.0 that you can interact with when you are retrieving information about a workbook structure. For more information, see the documentation included with the Open XML SDK 2.0 Productivity Tool: Click the Open XML SDK Documentation tab in the lower-left corner of the application window, and search for the class that you need to study. Although the documentation does not currently include code examples, given the sample shown here and the documentation, you should be able to successfully modify the sample application. |
> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/e53fbd55-03df-4a00-994b-db7e6657d498] Length: 00:06:55
About the Author Ken Getz, is a senior consultant with MCW Technologies. He is coauthor of ASP.NET Developers Jumpstart (Addison-Wesley, 2002), Access Developer's Handbook (Sybex, 2001), and VBA Developer's Handbook, 2nd Edition (Sybex, 2001). |