退出时的Powerpoint问题

时间:2013-10-29 16:05:06

标签: c# process interop powerpoint kill

我有一些c#代码打开一个Powerpoint幻灯片,刷新图表,然后退出。

这样可以正常工作,除非用户已经打开了Powerpoint幻灯片,在这种情况下,一旦exe运行,它将关闭他们现有的会话(丢失他们可能做出的任何更改!)。

所以,问题是如果你在后台打开PPT,运行运行以下代码的EXE(打开模板,刷新一些图表,保存和关闭),在应用程序退出时它会处理变量ppApp似乎关闭了每个正在运行的PPT。

如果我省略ppApp.Quit();

,情况就是如此
PowerPoint.Application ppApp = new PowerPoint.Application();                
PowerPoint.Presentation ppPres = ppApp.Presentations.Open(MITemplate, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

//refresh graphs
foreach(PowerPoint.Slide sl in ppPres.Slides)
{
    foreach (PowerPoint.Shape sh in sl.Shapes)
    {

        if (sh.Type.Equals(Microsoft.Office.Core.MsoShapeType.msoLinkedOLEObject))
        {
            sh.LinkFormat.SourceFullName = XLTemplate + sh.LinkFormat.SourceFullName.Substring(sh.LinkFormat.SourceFullName.LastIndexOf("\\") + 1);
            sh.LinkFormat.Update();
        }
    }
}
ppPres.SaveAs(outputFilename);
ppPres.Close(); 
ppApp.Quit();

1 个答案:

答案 0 :(得分:0)

您可以查看有多少其他演示文稿已打开,只有在您的演示文稿是唯一的演示文稿时才会退出:

ppPres.SaveAs(outputFilename);

var presentations = ppApp.Presentations;

if (presentations.Count <= 1)
{
    ppPres.Close(); 
    ppApp.Quit();
}