PowerPoint生成和活动演示文稿错误

时间:2011-01-04 21:03:23

标签: c# powerpoint

在我的Windows窗体应用程序中,我想用一张幻灯片生成一个简单的ppt文件并将一些文本转储到其中。应隐藏此过程(不启动PowerPoint或打开现有的ppt文件/模板),直到显示文件保存对话框。但是,文件保存操作会抛出一个异常,表示没有活动的演示文稿。有人可以对此有所了解吗?

    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);
    }

谢谢,

1 个答案:

答案 0 :(得分:1)

你应该SaveAs()ppt变量。