none
Looping through PresentationPart.SlidePart Error RRS feed

  • Question

  • I'm programatically manipulating a PowerPoint presentation and want to delete all the hidden slides. I'm using the Open XML SDK 2.0 and running into an issue where I'm trying to cycle through a collection to get the hidden slide - the entire foreach statement blows up inside the foreach loop as annotated. The idea is to get the slide index and pass it to a delete method. Can someone help please? Here is the code sniplet. Thanks!

    static void Main(string[] args)
    {
            //DeleteSlide(@"c:\temp\Default.pptx", 1);
            RetrieveNumberOfSlides(@"c:\temp\Default.pptx");
    }
    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();
                foreach (var x in presentationPart.SlideParts) 
                {
                   if (x.Slide.Show.HasValue != null)  <<< Null Reference Exception Here!
                   {
                      DeleteSlide(@"c:\temp\Default.pptx", <need slide index here>);
                   }
                }
             }
           }
         }
       }
       return slidesCount;
     }


    EJM

    Wednesday, November 7, 2012 6:08 PM

Answers

  • Hi EJM,

    Thanks for posting in the MSDN Forum.

    I found the Slide.Show will not set as usual. Only the slide has been hidden it will be false. So I would suggest you try to use "if(x.Slide.Show != null)" instead of your code.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 8, 2012 7:29 AM
    Moderator

All replies

  • Hi EJM,

    Thanks for posting in the MSDN Forum.

    I found the Slide.Show will not set as usual. Only the slide has been hidden it will be false. So I would suggest you try to use "if(x.Slide.Show != null)" instead of your code.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 8, 2012 7:29 AM
    Moderator
  • Hi EJM,

    Any update? Would you please tell me where you don't agree with my reply?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 5, 2012 5:58 AM
    Moderator