Close excel instance after creating .xls file in VB
Hi Everyone,
I am creating an excel file in my vb app. Below is the code I am using to do this. But when the application is closed, I still have an instance of Excel running in the background. How do i close it? I can do it with the xlWBook but not the xlApp. Thanks!
Code:
'create empty excel file
xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
xlWBook = CType(xlApp.Workbooks.Add, Microsoft.Office.Interop.Excel.Workbook)
xlWSheet = CType(xlWBook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
... do stuff here
xlWSheet.SaveAs(sFilePath)
xlWBook.Close()
How do I close to get rid of xlApp?
Thanks!
Re: Close excel instance after creating .xls file in VB
I already did .Quit() but in the Task Manager I still have EXCEL.EXE in there which was created by the vb.net app.
Re: Close excel instance after creating .xls file in VB
I use this...
Code:
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
Re: Close excel instance after creating .xls file in VB
Add this
vb Code:
xlWBook= Nothing
xlApp.Quit()
xlApp= Nothing
After
Re: Close excel instance after creating .xls file in VB
Nbrege has it right. You need to use the Marshal.ReleaseObject method. However, you will have to use it on every object you created (worksheets, workbooks, application) to make sure that everything shuts down properly. While typically just doing it on the App does the trick, sometimes Excel gets an attitude and doesnt want to play nice. Releasing all of the objects gives Excel a bit less of a temper.... :lol:
D
Re: Close excel instance after creating .xls file in VB
I was searching for a better way to close an excel application, and found this thread. Unfortunately, what nbrege suggested doesn't work in my case. My Excel app instance is created using late binding in a class. I was using that class to do some exporting in a BackgroundWorker thread. When the exporting was in the UI thread, then xlApp.Quit was sufficient. Now that the export is in a different thread, xlApp.Quit and ReleaseComObject(xlApp) do the same thing: Nothing. In both cases, there are no errors, no complaints, no objections, yet after the methods have executed (I stepped through that code, just to be as sure as I could be), Excel is still alive and kicking.
Any definitive solution?
Re: Close excel instance after creating .xls file in VB
The solution is GC.Collect.
Without adding that, none of what was suggested in this thread was sufficient. However, there are times when that GC.Collect is not necessary. In my case, some object was holding a reference and preventing Excel from closing, even though the object in question was garbage.
Re: Close excel instance after creating .xls file in VB
Shaggy: I tried this in my code and it excel closed a few seconds after the RunWorkerCompleted event fired. I found that Excel.exe stays open until I close my app, unless I explicitly call the Garbage Collector. Odds are my test app is so small the that garbage collector doesn't fire while it is running.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim xApp As New Excel.Application
Dim xWB As Excel.Workbook = xApp.Workbooks.Add()
Dim xSheet As Excel.Worksheet = DirectCast(xWB.Sheets.Add(), Excel.Worksheet)
xSheet.Cells(1, 1) = "A"
xWB.SaveAs("C:\Temp\testout.xls")
xApp.Quit()
Marshal.ReleaseComObject(xSheet)
Marshal.ReleaseComObject(xWB)
Marshal.ReleaseComObject(xApp)
xSheet = Nothing
xWB = Nothing
xApp = Nothing
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
GC.Collect()
End Sub
Re: Close excel instance after creating .xls file in VB
Yeah, I had to explicitly collect the garbage, as well, though I had a suitable location a bit after the thread closed.