How to: Retrieve the number of slides in a presentation document (Open XML SDK)
This topic shows how to use the classes in the Open XML SDK 2.5 for 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.
Last modified: March 25, 2013
Applies to: Office 2013 | Open XML
To use the sample code in this topic, you must install the Open XML SDK 2.5. You must explicitly reference the following assemblies in your project:
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.
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.
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.
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.
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 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.
The following is the complete RetrieveNumberOfSlides code sample in C# and Visual Basic.
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
Contribute to this article
Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub.