Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / Office / MS-Excel

Releasing Excel after using Interop

3.92/5 (5 votes)
22 Mar 2011CPOL 16.5K  
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:

C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)