简体   繁体   中英

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,

你应该 SaveAs() ppt变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM