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
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.5. 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.
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 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.
|
Contribute to this article Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub. |