|
-
Oct 28th, 2009, 12:02 PM
#1
Thread Starter
Frenzied Member
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!
-
Oct 28th, 2009, 12:05 PM
#2
Thread Starter
Frenzied Member
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.
-
Oct 28th, 2009, 12:20 PM
#3
Frenzied Member
Re: Close excel instance after creating .xls file in VB
I use this...
Code:
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
-
Oct 28th, 2009, 12:22 PM
#4
Re: Close excel instance after creating .xls file in VB
Add this
vb Code:
xlWBook= Nothing xlApp.Quit() xlApp= Nothing
After
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Oct 28th, 2009, 02:41 PM
#5
Fanatic Member
-
Oct 29th, 2009, 06:56 PM
#6
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?
My usual boring signature: Nothing
 
-
Oct 29th, 2009, 07:09 PM
#7
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.
My usual boring signature: Nothing
 
-
Oct 29th, 2009, 07:11 PM
#8
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
-
Oct 30th, 2009, 08:49 AM
#9
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.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|