Skip to content

Commit

Permalink
closes OfficeDev#194
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeebowen committed Nov 27, 2024
1 parent 545b059 commit 4346e18
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ms.suite: office
ms.author: o365devx
author: o365devx
ms.topic: conceptual
ms.date: 06/28/2021
ms.date: 11/27/2024
ms.localizationpriority: medium
---
# Retrieve the number of slides in a presentation document
Expand All @@ -21,7 +21,7 @@ This topic shows how to use the classes in the Open XML SDK 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
`RetrieveNumberOfSlides` method to illustrate
this task.


Expand All @@ -30,24 +30,18 @@ this task.

## RetrieveNumberOfSlides Method

You can use the **RetrieveNumberOfSlides**
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
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.

### [C#](#tab/cs-0)
```csharp
public static int RetrieveNumberOfSlides(string fileName,
bool includeHidden = true)
```
[!code-csharp[](../../samples/presentation/retrieve_the_number_of_slides/cs/Program.cs#snippet1)]

### [Visual Basic](#tab/vb-0)
```vb
Public Function RetrieveNumberOfSlides(ByVal fileName As String,
Optional ByVal includeHidden As Boolean = True) As Integer
```
[!code-vb[](../../samples/presentation/retrieve_the_number_of_slides/vb/Program.vb#snippet1)]
***


Expand All @@ -60,133 +54,81 @@ second parameter value. To call the method, pass all the parameter
values, as shown in the following code.

### [C#](#tab/cs-1)
```csharp
// 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));
```
[!code-csharp[](../../samples/presentation/retrieve_the_number_of_slides/cs/Program.cs#snippet2)]

### [Visual Basic](#tab/vb-1)
```vb
' 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))
```
[!code-vb[](../../samples/presentation/retrieve_the_number_of_slides/vb/Program.vb#snippet2)]
***



---------------------------------------------------------------------------------

## 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](/dotnet/api/documentformat.openxml.packaging.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](/dotnet/api/documentformat.openxml.packaging.presentationdocument.presentationpart) property to navigate to the main presentation part, storing the reference in a variable named **presentationPart**.
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 <xref:DocumentFormat.OpenXml.Packaging.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 <xref:DocumentFormat.OpenXml.Packaging.PresentationDocument.PresentationPart*> property to navigate to the main presentation part, storing the reference in a variable named `presentationPart`.

### [C#](#tab/cs-2)
```csharp
using (PresentationDocument doc =
PresentationDocument.Open(fileName, false))
{
// Get the presentation part of the document.
PresentationPart presentationPart = doc.PresentationPart;
// Code removed here…
}
Return slidesCount;
```
### [C#](#tab/cs)
[!code-csharp[](../../samples/presentation/retrieve_the_number_of_slides/cs/Program.cs#snippet3)]

### [Visual Basic](#tab/vb-2)
```vb
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
```
### [Visual Basic](#tab/vb)
[!code-vb[](../../samples/presentation/retrieve_the_number_of_slides/vb/Program.vb#snippet3)]
***



---------------------------------------------------------------------------------

## Retrieving the Count of All Slides

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](/dotnet/api/documentformat.openxml.packaging.presentationpart.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.

### [C#](#tab/cs-3)
```csharp
if (includeHidden)
{
slidesCount = presentationPart.SlideParts.Count();
}
else
{
// Code removed here…
}
```

### [Visual Basic](#tab/vb-3)
```vb
If includeHidden Then
slidesCount = presentationPart.SlideParts.Count()
Else
' Code removed here…
End If
```
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 <xref:DocumentFormat.OpenXml.Packaging.PresentationPart.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.

### [C#](#tab/cs)
[!code-csharp[](../../samples/presentation/retrieve_the_number_of_slides/cs/Program.cs#snippet4)]

### [Visual Basic](#tab/vb)
[!code-vb[](../../samples/presentation/retrieve_the_number_of_slides/vb/Program.vb#snippet4)]
***



---------------------------------------------------------------------------------

## Retrieving the Count of Visible Slides

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](/dotnet/api/documentformat.openxml.presentation.slide.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 scenarioPowerPoint does
include only those slides that have a <xref:DocumentFormat.OpenXml.Presentation.Slide.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 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](/dotnet/api/system.linq.enumerable.where)**
uses the <xref:System.Linq.Enumerable.Where*>
function with a lambda expression to do the work.

### [C#](#tab/cs-4)
```csharp
var slides = presentationPart.SlideParts.Where(
(s) => (s.Slide != null) &&
((s.Slide.Show == null) || (s.Slide.Show.HasValue &&
s.Slide.Show.Value)));
slidesCount = slides.Count();
```

### [Visual Basic](#tab/vb-4)
```vb
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()
```
### [C#](#tab/cs)
[!code-csharp[](../../samples/presentation/retrieve_the_number_of_slides/cs/Program.cs#snippet5)]

### [Visual Basic](#tab/vb)
[!code-vb[](../../samples/presentation/retrieve_the_number_of_slides/vb/Program.vb#snippet5)]
***


---------------------------------------------------------------------------------

## Sample Code

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

### [C#](#tab/cs)
[!code-csharp[](../../samples/presentation/retrieve_the_number_of_slides/cs/Program.cs)]
### [C#](#tab/cs-2)
[!code-csharp[](../../samples/presentation/retrieve_the_number_of_slides/cs/Program.cs#snippet0)]

### [Visual Basic](#tab/vb)
[!code-vb[](../../samples/presentation/retrieve_the_number_of_slides/vb/Program.vb)]
### [Visual Basic](#tab/vb-2)
[!code-vb[](../../samples/presentation/retrieve_the_number_of_slides/vb/Program.vb#snippet0)]

---------------------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The following code uses the **Descendants** generic method of the **Workbook** o
It's important to be aware that Excel supports two levels of worksheets. You can hide a worksheet by using the Excel user interface by right-clicking the worksheets tab and opting to hide the worksheet.
For these worksheets, the **State** property of the **Sheet** object contains an enumerated value of **Hidden**. You can also make a worksheet very hidden by writing code (either in VBA or in another language) that sets the sheet's **Visible** property to the enumerated value **xlSheetVeryHidden**. For worksheets hidden in this manner, the **State** property of the **Sheet** object contains the enumerated value **VeryHidden**.

Given the collection that contains information about all the sheets, the following code uses the **[Where](/dotnet/api/system.linq.enumerable.where)** function to filter the collection so that it contains only the sheets in which the **State** property is not null. If the **State** property is not null, the code looks for the **Sheet** objects in which the **State** property as a value, and where the value is either **SheetStateValues.Hidden** or **SheetStateValues.VeryHidden**.
Given the collection that contains information about all the sheets, the following code uses the <xref:System.Linq.Enumerable.Where*> function to filter the collection so that it contains only the sheets in which the **State** property is not null. If the **State** property is not null, the code looks for the **Sheet** objects in which the **State** property as a value, and where the value is either **SheetStateValues.Hidden** or **SheetStateValues.VeryHidden**.

### [C#](#tab/cs-5)
[!code-csharp[](../../samples/spreadsheet/retrieve_a_list_of_the_hidden_worksheets/cs/Program.cs#snippet2)]
Expand Down
18 changes: 15 additions & 3 deletions samples/presentation/retrieve_the_number_of_slides/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System;
using System.Linq;

// <Snippet0>
// <Snippet2>
if (args is [{ } fileName, { } includeHidden])
{
RetrieveNumberOfSlides(fileName, includeHidden);
Expand All @@ -10,25 +12,33 @@
{
RetrieveNumberOfSlides(fileName2);
}
// </Snippet2>

// <Snippet1>
static int RetrieveNumberOfSlides(string fileName, string includeHidden = "true")
// </Snippet1>
{
int slidesCount = 0;

// <Snippet3>
using (PresentationDocument doc = PresentationDocument.Open(fileName, false))
{
if (doc.PresentationPart is not null)
{
// Get the presentation part of the document.
PresentationPart presentationPart = doc.PresentationPart;
// </Snippet3>

if (presentationPart is not null)
{
if (includeHidden.ToLower() == "true")
// <Snippet4>
if (includeHidden.ToUpper() == "TRUE")
{
slidesCount = presentationPart.SlideParts.Count();
}
else
{
// </Snippet4>
// <Snippet5>
// 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.
Expand All @@ -37,6 +47,7 @@ static int RetrieveNumberOfSlides(string fileName, string includeHidden = "true"
((s.Slide.Show is null) || (s.Slide.Show.HasValue && s.Slide.Show.Value)));

slidesCount = slides.Count();
// </Snippet5>
}
}
}
Expand All @@ -45,4 +56,5 @@ static int RetrieveNumberOfSlides(string fileName, string includeHidden = "true"
Console.WriteLine($"Slide Count: {slidesCount}");

return slidesCount;
}
}
// </Snippet0>
63 changes: 41 additions & 22 deletions samples/presentation/retrieve_the_number_of_slides/vb/Program.vb
Original file line number Diff line number Diff line change
@@ -1,35 +1,54 @@
Imports DocumentFormat.OpenXml.Packaging
Imports System
Imports System.Linq

Module Program
Sub Main(args As String())
' <Snippet0>
' <Snippet2>
If args.Length = 2 Then
RetrieveNumberOfSlides(args(0), args(1))
ElseIf args.Length = 1 Then
RetrieveNumberOfSlides(args(0))
End If
' </Snippet2>
End Sub



Public Function RetrieveNumberOfSlides(ByVal fileName As String,
Optional ByVal includeHidden As Boolean = True) As Integer
' <Snippet1>
Function RetrieveNumberOfSlides(fileName As String, Optional includeHidden As String = "true") As Integer
' </Snippet1>
Dim slidesCount As Integer = 0
' <Snippet3>
Using doc As PresentationDocument = PresentationDocument.Open(fileName, False)
If doc.PresentationPart IsNot Nothing Then
' Get the presentation part of the document.
Dim presentationPart As PresentationPart = doc.PresentationPart
' </Snippet3>

If presentationPart IsNot Nothing Then
' <Snippet4>
If includeHidden.ToUpper() = "TRUE" Then
slidesCount = presentationPart.SlideParts.Count()
Else
' </Snippet4>
' <Snippet5>
' 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)))

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()
slidesCount = slides.Count()
' </Snippet5>
End If
End If
End If
End Using

Console.WriteLine($"Slide Count: {slidesCount}")

Return slidesCount
End Function
End Module
' </Snippet0>
End Module

0 comments on commit 4346e18

Please sign in to comment.