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: Microsoft PowerPoint 2010 | Microsoft Office 2010 | Microsoft Visual Studio 2010 | Microsoft Office PowerPoint 2007 | 2007 Microsoft Office system
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
Examining the Procedure
Public Function PPTGetSlideCount(ByVal fileName As String, Optional ByVal includeHidden As Boolean = True) As Integer
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
Accessing the Presentation
The code starts by creating an integer variable that can contain the return value, the number of slides that is named
Using doc As PresentationDocument = PresentationDocument.Open(fileName, False) ' Get the presentation part of the document. Dim presentationPart As PresentationPart = doc.PresentationPart ' Code removed here… End Using Return slidesCount
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.
If includeHidden Then slidesCount = presentationPart.SlideParts.Count() Else ‘ Code removed here… End If
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.
Dim slides = presentationPart.SlideParts. Where(Function(s) (s.Slide IsNot Nothing) AndAlso ((s.Slide.Show Is Nothing) OrElse (s.Slide.Show.HasValue AndAlso s.Slide.Show.Value))) slidesCount = slides.Count()
Public Function PPTGetSlideCount( ByVal fileName As String, Optional ByVal includeHidden As Boolean = True) As Integer Dim slidesCount As Integer = 0 Using doc As PresentationDocument = PresentationDocument.Open(fileName, False) ' Get the presentation part of the document. Dim presentationPart As PresentationPart = doc.PresentationPart If presentationPart IsNot Nothing Then If includeHidden Then slidesCount = presentationPart.SlideParts.Count() Else Dim slides = presentationPart.SlideParts. Where(Function(s) (s.Slide IsNot Nothing) AndAlso ((s.Slide.Show Is Nothing) OrElse (s.Slide.Show.HasValue AndAlso s.Slide.Show.Value))) slidesCount = slides.Count() End If End If End Using Return slidesCount End Function
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.
Watch the video
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).