65.9K
CodeProject is changing. Read more.
Home

Releasing Excel after using Interop

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.92/5 (5 votes)

Mar 22, 2011

CPOL
viewsIcon

16521

Alternatively, you can kill the Excel instance. I have to maintain an application which makes heavy use of Excel. I have written a wrapper for the base functionality. In the Dispose() method of the wrapper, I use the following code to ensure Excel will be released:mExcelApp.Quit(); // My...

Alternatively, you can kill the Excel instance. I have to maintain an application which makes heavy use of Excel. I have written a wrapper for the base functionality. In the Dispose() method of the wrapper, I use the following code to ensure Excel will be released:
mExcelApp.Quit(); // My Excel.Application instance
Process[] processes = Process.GetProcessesByName("Excel");
for (int i = 0; i < processes.Length;i++ )
{
    if(processes[i].Id == mProcessId)
    {
        processes[i].Kill();
        break;
    }
}
This will perfectly release the Excel instance I use with one exception. If I'm in Debug mode and cancel the application in the debugger, the instance is kept alive. Regards Klaus