Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't completely close MS Excel 2007...

Status
Not open for further replies.

dtvonly

Technical User
Jan 3, 2008
8
US
Hi. I don't know if this is a VB2005 or MS Excel issue so I will start with VB2005.

This is how I open an Excel file in VB2005:

Dim objExcel As New Excel.Application
Dim objWorkbook As Excel.Workbook
Dim objSheet As Excel.Worksheet
objExcel.Visible = True
objWorkbook = objExcel.Workbooks.Open("C:\Users\dvu\Documents\Data.xlsx")
objSheet = objWorkbook.Worksheets(1)

and this is how I close it:
Dim objExcel As New Excel.Application
objExcel.Application.Quit()

The problem is MS Excel always open a Book1.xlsx in addition to my Data.xlsx. As such closing Data.xlsx only closes Data.xlsx but not Book1.xlsx.
Over time, as I continue to develope and debug my program, Task Manager would collect "countless" Book1.xlsx. Wth EXCEL.EXE still opened, I noticed that my VB2005 doesn't work "completely" right.

If this is a VB2005 issue then how do I "completely" close everything that is Excel? If this is a Windows issue, and if you could offer a solution as well, I would truly appreciate any assistance. Thank you.
 

and this is how I close it:[red]
Dim objExcel As New Excel.Application[/red]
objExcel.Application.Quit()
Just a guess here, but why do you Dim new Excel before you close it? You Dim it already at the top of your code.

Have fun.

---- Andy
 
The Office Interop objects are COM objects, so you need to do some extra coding to release those objects. Use the System.Runtime.InteropServices.Marshal.FinalReleaseComObject method to do this:

Code:
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objSheet)

objWorkbook.Close(SaveChanges:=False)

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objWorkbook)

objExcel.Application.Quit()

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objExcel)

The calls to garbage collection (GC) are necessary; if you don't have them Excel will not be fully closed and will stay open.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you. I will try it.

On the same topic of closing, how do I "restart" a vb2005 form as though I am running it for the very first time without clicking the Exit button?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top