Sunday, January 25, 2009

Excel Runtimes not removed by Excel.Quit / Application.Quit

How to delete the leftover Excel object using VB.NET / Vb.NET delete Excel from memory

Excel runtimes that remain after the program is a nemesis. Many times this might be due to the leftover Excel objects in the program that causes Excel to remain in memory even after Application.Quit. Workbooks that are modified and left open often causes this problem. We have tried out one method to get rid of the Excel in memory using Vb.NET. We assumed that the programs that used Excel has been terminated and if any Excel remains in the memory it should be of the ‘legitimate’ application that the user has opened or the left over Excel.

We checked the presence of Title for Excel Application, which was present in all cases for the open instance of Excel. Left over instances didn’t had the title. The following code ‘kills’ the instances of Excel in memory that don’t have a WindowTitle.

Public Sub KillUnusedExcelProcess()

Dim oXlProcess As Process() = Process.GetProcessesByName("Excel")

For Each oXLP As Process In oXlProcess

If Len(oXLP.MainWindowTitle) = 0 Then

oXLP.Kill()

End If

Next

End Sub

Try if it works for you and post your suggestions or modifications

Keywords : automation does not close microsoft excel, Application.Quit not closing Excel

5 comments:

Praveen said...

Thank you very much.
helped me to kill all the hell Excel.exe files. i was working on this issues from past 4 hours, that why asp.net frmework fails to kill them even application closed. and finally i found that we are opening the Application by using Appplication class, after it creates an application and initiate the process EXCEL.exe then after onwards our application won't have any relationship with the process. So when we say application.Quit() it's not killing the process. Any way thanks for the post.

Anonymous said...

Great solution, though I needed to modify for C#. Here's the code:
private void KillUnusedExcel()
{
Process[] killUnused = Process.GetProcessesByName("Excel");
foreach (Process open in killUnused)
{
if (open.MainWindowTitle.Length == 0)
{
open.Kill();
}
}
}

woshinibaba said...

i really don't think this is a good idea. if the user is using Excel, what would happen??

Anonymous said...

This will kill all Excel processes from other .Net Excel instances. So be very careful...

Anonymous said...

this will kill all the excel processes which are noway connected with the code

kbAlertz.com :: Visual Studio 2005

kbAlertz.com :: Visual Studio 2008

kbAlertz.com :: Visual Basic 2005