Click here to Skip to main content
15,884,298 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Alternative
Tip/Trick

Releasing Excel after using Interop

Rate me:
Please Sign up or sign in to vote.
3.92/5 (5 votes)
22 Mar 2011CPOL 15.4K   5   8
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)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 . Pin
user464-Apr-11 11:18
user464-Apr-11 11:18 
GeneralReason for my vote of 1 If a user is running Excel and your ... Pin
wmjordan27-Mar-11 17:57
professionalwmjordan27-Mar-11 17:57 
GeneralReason for my vote of 2 I don't think that killing and relea... Pin
Igor Merabishvili23-Mar-11 3:32
Igor Merabishvili23-Mar-11 3:32 
GeneralRe: you are right. My experience was, that there are circumstanc... Pin
Klaus Luedenscheidt23-Mar-11 20:27
Klaus Luedenscheidt23-Mar-11 20:27 
GeneralReason for my vote of 3 I would strongly advice against kill... Pin
Karthik. A22-Mar-11 3:51
Karthik. A22-Mar-11 3:51 
GeneralRe: I've only posted the Dispose code. Naturally i'm releasing a... Pin
Klaus Luedenscheidt22-Mar-11 7:17
Klaus Luedenscheidt22-Mar-11 7:17 
GeneralReason for my vote of 2 I would strongly advice against kill... Pin
Karthik. A22-Mar-11 3:50
Karthik. A22-Mar-11 3:50 
GeneralI don't think that killing and releasing are the same things... Pin
Igor Merabishvili21-Mar-11 23:21
Igor Merabishvili21-Mar-11 23:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.