Search code examples
c#netoffice

Setting first slide to display using PowerPoint-Api


I use NetOffice.PowerPointApi to play some Powerpoint-Slides of an existing PPTX. This is how this is done:

PowerPoint.Application powerApplication = new PowerPoint.Application();
PowerPoint.Presentation presentation = powerApplication.Presentations.Open("C:\\dev\\test.pptx", MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoTrue);
// presentation.SlideShowSettings.StartingSlide = 2;
presentation.SlideShowSettings.Run();
while (powerApplication.ActivePresentation.SlideShowWindow.View.CurrentShowPosition < 4)
{
   System.Threading.Thread.Sleep(2000);
   powerApplication.ActivePresentation.SlideShowWindow.View.Next();
}

Now my plan was to display slide 3 to 4.

But when I set the startingSlide (commented out in my example) I receive an error on powerApplication.ActivePresentation.SlideShowWindow.View.CurrentShowPosition :

{"SlideShowView.CurrentShowPosition : Invalid request. There is currently no slide show view for this presentation."}

This only happens when I set the property StartingSlide. If I don't, the presentation runs from the first until the 4th slide.


Solution

  • You need to set more properties of the SlideShowSettings object:

    using NetOffice.OfficeApi.Enums;
    using NetOffice.PowerPointApi.Enums;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using PowerPoint = NetOffice.PowerPointApi;
    
    namespace PlayPowerPoint
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var app = new PowerPoint.Application())
                {
                    var presentation = app.Presentations.Open(Path.GetFullPath("Test.pptx"), MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
    
                    var slideShowSettings = presentation.SlideShowSettings;
    
                    slideShowSettings.StartingSlide = 2;
                    slideShowSettings.EndingSlide = 4;
                    slideShowSettings.RangeType = PpSlideShowRangeType.ppShowSlideRange;
                    slideShowSettings.AdvanceMode = PpSlideShowAdvanceMode.ppSlideShowManualAdvance;
    
                    slideShowSettings.Run();
    
                    var slideShowView = presentation.SlideShowWindow.View;
    
                    while (slideShowView.CurrentShowPosition < slideShowSettings.EndingSlide)
                    {
                        Thread.Sleep(2000);
                        slideShowView.Next();
                    }
    
                    presentation.Saved = MsoTriState.msoTrue;
                    presentation.Close();
    
                    app.Quit();
                }
            }
        }
    }