0

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?

Brad Rem
  • 6,036
  • 2
  • 25
  • 50
klashagelqvist
  • 1,251
  • 2
  • 26
  • 52

1 Answers1

2

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());
        }
S3ddi9
  • 2,121
  • 2
  • 20
  • 34
  • Good suggestion, one issue though there could be other instances of word opened by the user instead of by the application , i cant terminate them. I need to find out which process my application starts and terminate it. – klashagelqvist Sep 05 '12 at 22:33
  • 1
    `_applicationWD.Quit(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);` then add `System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_applicationWD);` – S3ddi9 Sep 05 '12 at 23:00