Powerpoint issue when quitting

蹲街弑〆低调 提交于 2019-12-20 04:06:25

问题


I have some c# code which opens a Powerpoint slide, refreshes the graphs, and then quits.

This works fine, unless the user already has a Powerpoint slide open, in which case it will close down their existing session (losing any changes they may have made!) once the exe has run.

So, the issue is if you have PPT’s open in the background, run my EXE which runs the below code (opening up a template, refreshing some graphs, saving and closing), upon application exit it disposes of the variable ppApp which seems to close down every running PPT.

This is even the case if I omit 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:


You could check how many other Presentations are open, and only quit if yours is the only one:

ppPres.SaveAs(outputFilename);

var presentations = ppApp.Presentations;

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


来源:https://stackoverflow.com/questions/19663478/powerpoint-issue-when-quitting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!