private static void QuitApplication(PowerPointInterop.Application powerPointApplication) { if (powerPointApplication != null) { try { powerPointApplication.DisplayAlerts = PowerPointInterop.PpAlertLevel.ppAlertsNone; powerPointApplication.Visible = Microsoft.Office.Core.MsoTriState.msoFalse; powerPointApplication.Quit(); Marshal.FinalReleaseComObject(powerPointApplication.Presentations); Marshal.FinalReleaseComObject(powerPointApplication); powerPointApplication = null; GC.Collect(); GC.WaitForPendingFinalizers(); // GC needs to be called twice in order to get the Finalizers called // - the first time in, it simply makes a list of what is to be // finalized, the second time in, it actually is finalizing. Only // then will the object do its automatic ReleaseComObject. GC.Collect(); } catch (Exception) { //Ignore } } }
public static bool CreateNewFile(IQuestion question, string filename) { WinUtil.DeleteFile(filename); if (File.Exists(filename)) return false; PowerPointInterop.Application powerpointApplication = null; try { //Avoid screen flickering or unwanted alerts while initializing powerpointApplication = new PowerPointInterop.Application(); // Create the Presentation File PowerPointInterop.Presentation pptPresentation = powerpointApplication.Presentations.Add(MsoTriState.msoTrue); pptPresentation.SaveAs(filename, PowerPointInterop.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTriStateMixed); pptPresentation.Close(); powerpointApplication.Quit(); return true; } catch (Exception) { return false; } finally { QuitApplication(powerpointApplication); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)