klashagelqvist
klashagelqvist

Reputation: 1261

Checking if Word has terminated correctly

I'm automating a Word document from a WPF application in C#.

In the application I create a Word object with

_applicationWD = new Microsoft.Office.Interop.Word.Application();

and I terminate it with

_applicationWD.Quit(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);

Everything is neatly put in try/catch statements. As Quit is the last thing that can go wrong, I need to check if the Quit statement succeeded and then try to terminate it in some other way, otherwise I will have a lot of WinWord.exe in the process list.

Is there any way to check if Word closed or maybe get the PID of the process and force it to terminate from code?

Upvotes: 0

Views: 475

Answers (1)

S3ddi9
S3ddi9

Reputation: 2151

this should kill all the winword processes

        try
        {
            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("WinWord");
            foreach (var myproc in procs)
                myproc.Kill();


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }

Upvotes: 2

Related Questions