0

I have following set of code in my program to set caption to word application and trying to close winword.exe process which is having same title.

Application wordApp=new Microsoft.Office.Interop.Word.Application()
wordApp.Visible = true;                    
wordApp.Application.Caption = "Test Document";
System.Diagnostics.Process[] aProcWrd = System.Diagnostics.Process.GetProcessesByName("WINWORD");
foreach (System.Diagnostics.Process oProc in aProcWrd)
{
 if (oProc.MainWindowTitle=="Test Document")
   oProc.Kill();
}

But Process.MainWindowTitle is always coming as empty.

leppie
  • 115,091
  • 17
  • 196
  • 297

1 Answers1

0

oProc.kill() is not really an elegant solution. Instead of closing word you are waking it out of your computer with a sledge hammer. Great for when a program is frozen, but not really mend for closing a program that is still working as intended.

A more clean solution would be to cast your found processes to a world object and ask them to close (it also gives the user an option to save his work then instead of the window just disheartening). A example about how to do that can be found here: how to close a running instance of Word document? (C#)

Community
  • 1
  • 1
Nick Otten
  • 702
  • 7
  • 17