From 4346e18761903dc9bad891a7d40a72c2e0f98d20 Mon Sep 17 00:00:00 2001 From: Michael Bowen Date: Wed, 27 Nov 2024 13:54:50 -0800 Subject: [PATCH] closes #194 --- ...er-of-slides-in-a-presentation-document.md | 138 +++++------------- ...-the-hidden-worksheets-in-a-spreadsheet.md | 2 +- .../cs/Program.cs | 18 ++- .../vb/Program.vb | 63 +++++--- 4 files changed, 97 insertions(+), 124 deletions(-) diff --git a/docs/presentation/how-to-retrieve-the-number-of-slides-in-a-presentation-document.md b/docs/presentation/how-to-retrieve-the-number-of-slides-in-a-presentation-document.md index 1808f843..04333fb2 100644 --- a/docs/presentation/how-to-retrieve-the-number-of-slides-in-a-presentation-document.md +++ b/docs/presentation/how-to-retrieve-the-number-of-slides-in-a-presentation-document.md @@ -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 @@ -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. @@ -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)] *** @@ -60,118 +54,66 @@ 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 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 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 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 scenario—PowerPoint does +include only those slides that have a 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 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)] *** @@ -179,14 +121,14 @@ function with a lambda expression to do the work. ## 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)] --------------------------------------------------------------------------------- diff --git a/docs/spreadsheet/how-to-retrieve-a-list-of-the-hidden-worksheets-in-a-spreadsheet.md b/docs/spreadsheet/how-to-retrieve-a-list-of-the-hidden-worksheets-in-a-spreadsheet.md index 21abf5a9..e045345d 100644 --- a/docs/spreadsheet/how-to-retrieve-a-list-of-the-hidden-worksheets-in-a-spreadsheet.md +++ b/docs/spreadsheet/how-to-retrieve-a-list-of-the-hidden-worksheets-in-a-spreadsheet.md @@ -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 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)] diff --git a/samples/presentation/retrieve_the_number_of_slides/cs/Program.cs b/samples/presentation/retrieve_the_number_of_slides/cs/Program.cs index 60a5eba8..810875b0 100644 --- a/samples/presentation/retrieve_the_number_of_slides/cs/Program.cs +++ b/samples/presentation/retrieve_the_number_of_slides/cs/Program.cs @@ -2,6 +2,8 @@ using System; using System.Linq; +// +// if (args is [{ } fileName, { } includeHidden]) { RetrieveNumberOfSlides(fileName, includeHidden); @@ -10,25 +12,33 @@ { RetrieveNumberOfSlides(fileName2); } +// +// static int RetrieveNumberOfSlides(string fileName, string includeHidden = "true") +// { int slidesCount = 0; - + // using (PresentationDocument doc = PresentationDocument.Open(fileName, false)) { if (doc.PresentationPart is not null) { // Get the presentation part of the document. PresentationPart presentationPart = doc.PresentationPart; + // + if (presentationPart is not null) { - if (includeHidden.ToLower() == "true") + // + if (includeHidden.ToUpper() == "TRUE") { 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. @@ -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(); + // } } } @@ -45,4 +56,5 @@ static int RetrieveNumberOfSlides(string fileName, string includeHidden = "true" Console.WriteLine($"Slide Count: {slidesCount}"); return slidesCount; -} \ No newline at end of file +} +// diff --git a/samples/presentation/retrieve_the_number_of_slides/vb/Program.vb b/samples/presentation/retrieve_the_number_of_slides/vb/Program.vb index 811b36b2..5a42185f 100644 --- a/samples/presentation/retrieve_the_number_of_slides/vb/Program.vb +++ b/samples/presentation/retrieve_the_number_of_slides/vb/Program.vb @@ -1,35 +1,54 @@ Imports DocumentFormat.OpenXml.Packaging +Imports System +Imports System.Linq Module Program Sub Main(args As String()) + ' + ' + If args.Length = 2 Then + RetrieveNumberOfSlides(args(0), args(1)) + ElseIf args.Length = 1 Then + RetrieveNumberOfSlides(args(0)) + End If + ' End Sub - - - Public Function RetrieveNumberOfSlides(ByVal fileName As String, - Optional ByVal includeHidden As Boolean = True) As Integer + ' + Function RetrieveNumberOfSlides(fileName As String, Optional includeHidden As String = "true") As Integer + ' Dim slidesCount As Integer = 0 + ' + 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 + ' + + If presentationPart IsNot Nothing Then + ' + If includeHidden.ToUpper() = "TRUE" 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))) - 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() + ' + End If End If End If End Using + + Console.WriteLine($"Slide Count: {slidesCount}") + Return slidesCount End Function -End Module \ No newline at end of file + ' +End Module