Can't close Excel with VisualBasic 2005

I'm using excel.application in visualBasic 2005, and I don't manage to close it. I tried some solutions found on the net, and appart the one killing the processus "Excel" in the task manager, no solution seem to work.

In this simple example, Excel disapear from the task manager, but with the line in comment activated, excel stays open. Launching the function a second time create a new Excel Process, but close it after (one is still staying in the task manager).

Please, help me to close the processus.

Dim xlsheet As Excel.Worksheet

Dim xlBook As Excel.Workbook

Dim xlapp As Excel.Application

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

xlapp = New Excel.Application

xlBook = xlapp.Workbooks.Open("test")

Dim text as string

xlsheet = xlBook.Worksheets("sheet1")

text = xlsheet.Cells(1, 1).value

text = xlsheet.Cells(1, 1).value

text = xlsheet.Cells(1, 1).value

text = xlsheet.Cells(1, 1).value

text = xlsheet.Cells(1, 1).value

text = xlsheet.Cells(1, 1).value

text = xlsheet.Cells(1, 1).value

'text = xlsheet.Cells(1, 1).value

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsheet)

xlsheet = Nothing

xlBook.Close(False)

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)

xlBook = Nothing

xlapp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp)

xlapp = Nothing

GC.Collect()

GC.WaitForPendingFinalizers()

GC.Collect()

GC.WaitForPendingFinalizers()

End Sub



Answer this question

Can't close Excel with VisualBasic 2005

  • bahadir

    Thank you.

    With the loop you have copied and paste, excel is closing for me too, but only when I add the lines for the Garbage Collection at the end. The problem comes when you uncomment the line " 'text = xlsheet.Cells(1, 1).value " . In this case, one Excel processus stays open in the task manager. If you repeat the same line some other times, it will stay open. You can try by looping the line as:

    for i as integer = 0 to ...

    text = xlsheet.Cells(1, 1).value

    next

    You will see that when you reach a certain number of iterations, excel doesn't close anymore. It's really strange because I tried the same test with the line " text = xlsheet.Name() " and the same problem is happening. I need to repeat some line of code until 5000 times in some case.

    But there is a difference between your code and mine: when I put " Imports Microsoft.Office.Interop " it gives me the error :

    " Namespace or type specified in the Imports 'Microsoft.Office.Interop' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases. "

    In fact I'm working adding to the reference: " Microsoft Excel 11.0 Object Library " in order to have the code working.

    Tell me if you get the problem.


  • vidhyaprakash

    Excel 2003.
  • NewbieDude

    Somebody knows about this strange mistake How to make it work
  • Xadja

    I'm a little unsure what your getting at.

    the following code...

    Imports Microsoft.Office.Interop

    Public Class Form1
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    For i = 1 To 10
    Dim xlsheet As Excel.Worksheet
    Dim xlBook As Excel.Workbook
    Dim xlapp As Excel.Application

    xlapp = New Excel.Application
    xlBook = xlapp.Workbooks.Open("d:\test.xls")
    Dim text As String
    xlapp.Visible = True
    xlsheet = xlBook.Worksheets("sheet1")
    text = xlsheet.Cells(1, 1).value
    text = xlsheet.Cells(1, 1).value
    text = xlsheet.Cells(1, 1).value
    text = xlsheet.Cells(1, 1).value
    text = xlsheet.Cells(1, 1).value
    text = xlsheet.Cells(1, 1).value
    text = xlsheet.Cells(1, 1).value
    'text = xlsheet.Cells(1, 1).value

    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsheet)
    xlsheet = Nothing
    xlBook.Close(False)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
    xlBook = Nothing
    xlapp.Quit()
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp)
    xlapp = Nothing

    Next
    End Sub


    End Class

    This is you code in a loop so its opening and closing Excel and as far as I can tell at the end of it - excel is no longer loaded. I looked in task magaer for an excel process or task running and there is nothing. So I would think that it closed and no longer in memory at all.

    Unless your seeing something different here - perhaps you need to provide a bit more information to allow us to repro the problem.


  • ftamminga

    I'm also using Excel 2003.

    II commented the line in and didnt have the garbage collector lines, increased the count to 200 to really repeatedly open and close the object trying to get somethign weird to happen with the reference counting which may leave cause the application to remain open and I still didnt get anything left open at the end - The OS I'm using is Windows 2003 but I'll try again using XP as I know I've seen weird stuff occuring on the COM interop before with Office products.

    I'd probably wrap it in a try catch to verify that no exception is getting thrown and if one is have a suitable process for cleaning up. Also the Garbage collector calls probably arent going to help clean up the Excel instances because Excel is COM based and doesnt use a garbage collection process but uses reference counting to determine when to unload itself. Hence thats why I didnt put them in my code.

    Perhaps a little more info on your OS Machine configuration to see if I can track this down. Does this repro every single time or is it a little unpredicatbale when it occurs.


  • gregg100

    Hi, I have the same problem in my Excel project. I have tried the GC solution but the problem doesn't disapear. Im my case, sometimes Excel closes (rarely).


  • kennm

    My Os is: Windows XP, Home Edition, Version 2002, Service Pack 2

    The error is happening every single time.

    It's strange, because without the GC lines, none of the excel process closes at anytime; for example with your code we will finish with 9 Excel processes open until we close the form.The lines that I've writen for the GC are:

    GC.Collect()

    GC.WaitForPendingFinalizers()

    Writing those line a second time (as I saw on the forums or web sites about VB) doesn't change anything in my case.

    Thanks


  • sql dev

    Spotty,

    Have you found something interesting on this problem Tell me if you have managed to reproduce / solve the error. I need to use this functionality in my application.

    Thank you for your help.


  • ATony

    what version of excel are you using
  • Dany V

    Hi Spotty!

    Have you found a solution to this Excel Interop problem Have you managed to reproduce it on XP I worked on it, without any result for now.

    Let me know if you find something.


  • vicarious

    OK I will try and repro Using XP SP2 and Excel 2003 and see if I can repro it. Something weird going on here....


  • Can't close Excel with VisualBasic 2005