How to: Retrieve the Number of Slides in a Presentation Document

This topic shows how to use the classes in the Open XML SDK 2.0 for Microsoft Office to programmatically retrieve the number of slides in a presentation document, either including hidden slides or not, without loading the document into Microsoft PowerPoint. It contains an example RetrieveNumberOfSlides method to illustrate this task.

Applies to: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

In this article
RetrieveNumberOfSlides Method
Calling the RetrieveNumberOfSlides Method
How the Code Works
Retrieving the Count of All Sheets
Retrieving the Count of Visible Sheets
Sample Code

To use the sample code in this topic, you must install the Open XML SDK 2.0. You must explicitly reference the following assemblies in your project:

  • WindowsBase

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

You must also use the following using directives or Imports statements to compile the code in this topic.

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

RetrieveNumberOfSlides Method

You can use the RetrieveNumberOfSlides method to get the number of slides in a presentation document, optionally including the hidden slides. The RetrieveNumberOfSlides method accepts two parameters: a string that indicates the path of the file that you want to examine, and an optional Boolean value that indicates whether to include hidden slides in the count.

public static int RetrieveNumberOfSlides(string fileName, 
    bool includeHidden = true)
Public Function RetrieveNumberOfSlides(ByVal fileName As String,
        Optional ByVal includeHidden As Boolean = True) As Integer

Calling the RetrieveNumberOfSlides Method

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. To call the method, pass all the parameter values, as shown in the following code.

// Retrieve the number of slides, excluding the hidden slides.
Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH, false));
// Retrieve the number of slides, including the hidden slides.
Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH));
' Retrieve the number of slides, excluding the hidden slides.
Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH, False))
' Retrieve the number of slides, including the hidden slides.
Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH))

How the Code Works

The code starts by creating an integer variable, slidesCount, to hold the number of slides. The code then opens the specified presentation by using the PresentationDocument.Open method and indicating that the document should be open for read-only access (the final false parameter value). 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;
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 the Count of All Sheets

If the presentation part reference is not null (and it will not be, for any valid presentation that loads correctly into PowerPoint), the code next calls the Count method on the value of the SlideParts property of the presentation part. If you requested all slides, including hidden slides, that is all there is to do. There is slightly more work to be done if you want to exclude hidden slides, as shown in the following code.

if (includeHidden)
{
    slidesCount = presentationPart.SlideParts.Count();
}
else
{
    // Code removed here…
}
If includeHidden Then
    slidesCount = presentationPart.SlideParts.Count()
Else
    ' Code removed here…
End If

Retrieving the 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 the value 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 following code uses the Where function 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();
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()

Sample Code

The following is the complete RetrieveNumberOfSlides code sample in C# and Visual Basic.

public static int RetrieveNumberOfSlides(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
            {
                // Each slide can include a Show property, which if hidden 
                // will contain the value "0". The Show property may not 
                // exist, and most likely will not, for non-hidden slides.
                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;
}
Public Function RetrieveNumberOfSlides(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
                ' Each slide can include a Show property, which if 
                ' hidden will contain the value "0". The Show property may 
                ' not exist, and most likely will not, for non-hidden slides.
                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

See Also

Reference

Class Library Reference

Other Resources

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