Windows Phone Developers

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

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

8 comments:

  1. 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.

    ReplyDelete
  2. 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();
    }
    }
    }

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

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

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

    ReplyDelete
  6. Excellent....helped a lot. I was checking from last 5 hours

    ReplyDelete
  7. For 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..

    Instead 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()

    ReplyDelete
  8. this sequence works just fine:

    Marshal.FinalReleaseComObject(xlsSheet)
    Marshal.FinalReleaseComObject(xlsWB)
    xlsApp.Quit()
    Marshal.FinalReleaseComObject(xlsApp)
    'Garbage collecting
    GC.Collect()
    GC.WaitForPendingFinalizers()

    ReplyDelete