In the namespace section add this using statement.
using System.Diagnostics;
This example instantiated Excel with this:
_Application excel = new _Excel.Application();
This method kills the right Excel task by using the window handle.
public void Kill()
{
Int32 ExcelHwnd = excel.Hwnd;
Process[] localExcel = Process.GetProcessesByName("EXCEL");
foreach (Process Pgm in localExcel)
{
// xlMinimized keeps the screen from flashing when the user interface is made
// visible with the excel.visible needed to set the MainWindowHandle
excel.WindowState = XlWindowState.xlMinimized;
excel.Visible = true;
if ((Pgm.ProcessName == "EXCEL") && (ExcelHwnd == Pgm.MainWindowHandle.ToInt32()))
{
Pgm.Kill();
}
}
}
This worked without fail.