Question

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();
Was it helpful?

Solution

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top