Search code examples
c#powerpoint

PowerPoint Generation and Active Presentation Error


In my windows form app, I want to generate a simple ppt file with one slide and dump some text into it. This process should be hidden(without launching PowerPoint or opening existing ppt file/template) until file save dialog shows up. However, file-save operation throws an exception saying there's no active presentation. Can someone shed some lights on this?

    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Office = Microsoft.Office.Core;
    ...
    PowerPoint.Application pptApp = new PowerPoint.Application();
    PowerPoint.Presentations ppts = pptApp.Presentations;
    PowerPoint.Presentation ppt = ppts.Add(Office.MsoTriState.msoTrue);
    PowerPoint.Slides slides = ppt.Slides;
    PowerPoint.Slide slide = slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank);
    PowerPoint.Shapes shapes = slide.Shapes;
    PowerPoint.Shape shape = shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 0, 0, 500, 50);
    shape.TextFrame.TextRange.InsertAfter("foo");

    System.Windows.Forms.SaveFileDialog fd = new System.Windows.Forms.SaveFileDialog();
    if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {            
        //This line throws an exception: Invalid request. There is no active presentation.
        pptApp.ActivePresentation.SaveAs(fd.FileName,PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, Office.MsoTriState.msoTrue);
    }

Thanks,


Solution

  • You should SaveAs() the ppt variable.