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!

Excel in VB.NET problem 1

Status
Not open for further replies.

Tomeczek

Programmer
Oct 25, 2012
40
US
My latest project was a VB.NET program, which gathers information from different machines in the domain and puts collected data into Excel spreadsheet.
The problem I have is the Excel part. I was able to open new or existing Excel file, place data on multiple worksheets and save it.
I realized, though, that after exiting the program, the EXCEL.EXE *32 process is still running. The funny part of it is that when running program from within the “Visual Studio 2010”, the EXCEL process disappears after program’s completion. I have in my code Marshal.ReleaseComObject calls for each Excel object opened (Excel, workbook(s), worksheet(s)) – but it only works correctly within the VS2010.
Any ideas why it happens?
 

Here's how I close Office Interop objects when I'm done with them:

If oSheet IsNot Nothing Then
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oSheet)
End If

If oBook IsNot Nothing Then
oBook.Close(False, Type.Missing, Type.Missing)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oBook)
End If

If oExcel IsNot Nothing Then
oExcel.Quit()
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oExcel)
End If

GC.Collect()
GC.WaitForPendingFinalizers()

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 for your response!

I do more or less the same. Don't have GC.Collect() and GC.WaitForPendingFinalizers(), though. I'll add these two and try again.
Thanks!
 
Thank you, jebenson!
It worked.

Looks like the order of closing/releasing of objects should be:
- oSheet
- oWorkBook
- oExcel

I did that, added GC.Collect() and GC.WaitForPendingFinalizers() and finally I got it working!

Thanks!
 
To show the appreciation for help received, it is customary on TT to use:
[blue]
Like this post?
Star it!
[/blue]

It also says to others: this post was helpful.

Podziekuj :)

Have fun.

---- Andy
 
All done!
This is a great idea, I wish I was aware of it before...

------------------------------------------------------------------------
Naprawdę nie zorientowałem się o tej możliwości. Byłbym to robił wcześniej po otrzymaniu zbawiennych rad.
Dzięki, Andrzej!
[smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top