1

I have an object of Excel, sheet.Excel.Application eApp.

How do I get the process ID of this Excel application and how do I kill that process?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1058182
  • 27
  • 1
  • 7
  • 1
    Obligatory Old New Thing link: http://blogs.msdn.com/b/oldnewthing/archive/2011/11/18/10238335.aspx – Mark Pim Nov 28 '11 at 14:05
  • You should be able to call `ExcelApp.Quit()`. See [this link](http://www.craigmurphy.com/blog/?p=82) – Mark Pim Nov 28 '11 at 14:06
  • Calling Quit() on the application object often does not work, so this is not a very good answer. There is a perfectly good reason for wanting the process id so as to be able to kill Excel reliably when it goes out to lunch. – Noah Yetter Feb 29 '12 at 05:14

2 Answers2

2
var app = new ApplicationClass();

...

app.Quit();

See MSDN

abatishchev
  • 98,240
  • 88
  • 296
  • 433
1

In addition to the normal Quit method of the ApplicationClass class, you can try the below, though it is subject to some, but not all, of the pitfalls mentioned in the link @Mark Pim suggested.

System.Diagnostics.Process[] myProcesses;
// Returns array containing all instances of Excel.
myProcesses = System.Diagnostics.Process.GetProcessesByName("Excel");
foreach (System.Diagnostics.Process myProcess in myProcesses)
{
    if (myProcess.MainWindowTitle == Globals.ThisWorkbook.Application.Caption)
    {
        myProcess.Kill();
    }
}
Peter Majeed
  • 5,304
  • 2
  • 32
  • 57