Export (0) Print
Expand All
Expand Minimize
1 out of 2 rated this helpful - Rate this topic

Retrieving the Number of Slides from PowerPoint 2010 Presentations by Using the Open XML SDK 2.0

Office 2010

Office Visual How To

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

Overview

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.

Code It

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:

  • WindowsBase (this reference may already be set for you, depending on the kind of project that you create)

  • DocumentFormat.OpenXml (installed by the Open XML SDK 2.0)

Also ensure that your code includes at least the following using/Imports statements at the top of the code file:

using System.Linq;
using DocumentFormat.OpenXml.Packaging;

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).

public static int PPTGetSlideCount(string fileName,
    bool includeHidden = true)

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.

Console.WriteLine(PPTGetSlideCount(DEMOPATH, false));

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 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.

using (PresentationDocument doc = PresentationDocument.Open(fileName, false))
{
    // Get the presentation part of the document.
    PresentationPart presentationPart = doc.PresentationPart;
    // Code removed here…
}
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)
{
    slidesCount = presentationPart.SlideParts.Count();
}
else
{
    // Code removed here…
}

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.

var slides = presentationPart.SlideParts.Where(
    (s) => (s.Slide != null) && 
      ((s.Slide.Show == null) || 
       (s.Slide.Show.HasValue && s.Slide.Show.Value)));
slidesCount = slides.Count();

Sample Procedure

public static int PPTGetSlideCount(string fileName, 
    bool includeHidden = true)
{
    int slidesCount = 0;

    using (PresentationDocument doc = PresentationDocument.Open(
        fileName, false))
    {
        // Get the presentation part of the document.
        PresentationPart presentationPart = doc.PresentationPart;
        if (presentationPart != null)
        {
            if (includeHidden)
            {
                slidesCount = presentationPart.SlideParts.Count();
            }
            else
            {
                var slides = presentationPart.SlideParts.Where(
                    (s) => (s.Slide != null) && 
                    ((s.Slide.Show == null) || 
                    (s.Slide.Show.HasValue && s.Slide.Show.Value)));
                slidesCount = slides.Count();
            }
        }
    }
    return slidesCount;
}
Read It

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.

See It

Watch the video

Watch video

Length: 00:06:55

Click to grab code

Grab the Code

Explore It

 

About the Author

MVP Contributor  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).

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.