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
Thank you very much.
ReplyDeletehelped 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.
Great solution, though I needed to modify for C#. Here's the code:
ReplyDeleteprivate void KillUnusedExcel()
{
Process[] killUnused = Process.GetProcessesByName("Excel");
foreach (Process open in killUnused)
{
if (open.MainWindowTitle.Length == 0)
{
open.Kill();
}
}
}
i really don't think this is a good idea. if the user is using Excel, what would happen??
ReplyDeleteThis will kill all Excel processes from other .Net Excel instances. So be very careful...
ReplyDeletethis will kill all the excel processes which are noway connected with the code
ReplyDeleteExcellent....helped a lot. I was checking from last 5 hours
ReplyDeleteFor me the problem was in the For Next loop were xlSheet is left hanging in COM and Excel instance never quits. So this is what solved my situation with Excel not closing..
ReplyDeleteInstead of writing like this..
-----------------------------
Dim xlWorkbook As Excel.Workbook
xlWorkbook = xlApp.Workbooks.Open("file.xls")
For Each xlSheet as Excel.Worksheet In xlWorkbook.Sheets
'do stuff here
Next
Code that worked:
---------------------------
Dim xlWorkbook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlWorkbook = xlApp.Workbooks.Open("file.xls")
For Each xlSheet In xlWorkbook.Sheets
'do stuff here
Next
'properly quit and clean up
xlWorkbook.Close()
Marshal.FinalReleaseComObject(xlWorkbook)
Marshal.FinalReleaseComObject(xlSheet)
xlApp.Quit()
Marshal.ReleaseComObject(xlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
this sequence works just fine:
ReplyDeleteMarshal.FinalReleaseComObject(xlsSheet)
Marshal.FinalReleaseComObject(xlsWB)
xlsApp.Quit()
Marshal.FinalReleaseComObject(xlsApp)
'Garbage collecting
GC.Collect()
GC.WaitForPendingFinalizers()