I created a excel helper class, to interact excel interop service.
But i noticed that the excel.exe is not getting closed on the server.
(windows 2008 64bit Japanese OS and office 2007 32bit).
When i checked with process explorer it shows tooltip like:
Path:[Error opening process]
I did excel.Quit() and Marshal.FinalReleaseComObject(_xlApp) but nothing works as expected, then tried to kill the process by processID, still not killing the process.
uint processID = 0;
GetWindowThreadProcessId((IntPtr)_hWnd, out processID);
if (processID != 0)
{
System.Diagnostics.Process.GetProcessById((int)processID).Kill();
}
Then i tried below both method, but it close all manually opened excel documents.
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process p in procs)
{
int baseAdd = p.MainModule.BaseAddress.ToInt32();
if (baseAdd == _xlApp.Hinstance)
{
p.Kill();
}
}
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process p in procs)
{
if (p.MainWindowTitle.Length == 0)
{
p.Kill();
}
}
Any idea about how to deal with this case ?
To get the processId is a little bit more complicated.
Try this…
That said, violence is just the last resourse 😉
You need to kill because there are some COM objects not released. (See
MS Support: Office application does not close)
Try reference your COM objects always, put them in a Stack and release them after use with
then, a simple
application.Quit();application = nullwill make the trick.Hope it helps.
EDIT:
– Always means: “whenever you use two points (
_xlApp.Application., _xlWorkbook.Worksheets,...)Add to stack means
stack.push(_xlApp.Application)Release means
stack.pop()I include mi helperStack
}