Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

We are automating MsOffice in winform application(C#).

Currently we are working on, to create a powerpoint file(Programmatically). We are successfully created pptx file.

After creating a pptx file, we are trying to close both the presentation and application. Its clsoing only presentation, but not ppt application.

MyCode

What I have tried:

C#
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
                }
            }
        }


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


Ref: Office C# app automates PowerPoint (CSAutomatePowerPoint) sample in C# for Visual Studio 2010[^]

can anyone please help me.

Thanks
Posted
Updated 9-May-17 20:17pm
v3
Comments
CHill60 7-May-17 5:16am    
Remove the try - catch blocks so you can tell if an exception is being raised
abdul subhan mohammed 7-May-17 5:29am    
I have tried removing try-catch block, but no exception has been raised.
Michael_Davies 7-May-17 5:45am    
Whenever I use ComObject I find I have to loop until the release returns zero, never used final so not sure if there is a difference but a single call does not always release the COM link;

While System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel) <> 0
Application.DoEvents()
End While
Barry_Sharp 15-Jan-18 4:45am    
Just as an FYI, you could consider avoiding the hassle that comes with COM interface by using a pure C# solution. For example, you may want to check out this PowerPoint library in C# for presentations. For instance, here is how you would create a new presentation with it:

PresentationDocument presentation = new PresentationDocument();
presentation.Save("empty-presentation.pptx");

1 solution

I'd suggest to read this: Office application does not quit after automation from Visual Studio .NET client[^]
You have to "clean up" by using Marshal.ReleaseComObject Method (Object) (System.Runtime.InteropServices)[^] directly on your ppt application object (instance).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900