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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to Excel - Can't quit Excel.exe process 2

Status
Not open for further replies.

miket26

Programmer
Joined
Apr 13, 2004
Messages
63
Location
CA
Hello,

I'm writing to Excel in my program successfully, but after I finish, quit Excel and set my variables to nothing in the program's method, the Excel.exe process is still running in Windows. It terminates when I close my program, but not when I try to kill it in the method I'm using. I can't figure out why the Excel.exe is still running. If anyone can help, thanks in advance. Also, the method is running in a thread, so I'm not sure if that has something to do with the problem (the thread is tested and works great).

Here's the code I'm trying to get to work...

Public Sub tryExcel()
Dim objExcel As Object
Dim objBook As Object
Dim objSheet As Object

objExcel = CreateObject("Excel.Application")
objBook = objExcel.Workbooks.Add
objSheet = objBook.Worksheets(1)

objSheet.cells(1, 1) = "Hello World"

objBook.SaveAs("C:\myexcel.xls")

objSheet = Nothing
objBook = Nothing
objExcel.Quit()
objExcel = Nothing
End Sub
 
Try adding
Code:
objBook.Close(False, Type.Missing, Type.Missing)
after saving the workbook. The open workbook may be preventing the Excel process from quiting.
 
Thanks for the help, but I still can't get it to stop running the Excel.exe process once the method is finished. It only stops running once I terminate the program, so it is somehow connected to the Excel application that is running. I've tried closing the workbook, and releasing all of the objects, yet neither work. Anyone face this problem before? It seems that there is a lot of code on outputting to Excel, but there is no mention of this sort of problem. I'm wondering if its a 'Windows' or configuration problem. Thanks.
 
That is manily due to a memory leak in Excel Object and I had to do a GC.Collect to remove the Excel.Exe.

-Kris
 
Thanks Kris, ive had the same problem, I thought gc was mostly automatice in .net but after reading your solution
I looked a little deeper and there are a lot of graphics
intensive "stuff" you have to force clean up on.



if it is to be it's up to me
 
Yes, it is true that GC is always automated and is not a good idea to call GC.Collect all the time. But this memory leak is something microsoft acknowledged and the suggestion was to call GC.Collect to remove the object from memory. I tried to find out the link in MSDN about this but was not able to get it now. If I find it I will post that link here.

-Kris
 
Thanks for the tip. Its a relief that the problem is actually this easy to correct... I initially thought the problem was much deeper. I had assumed that setting the variables to nothing would destroy the object and everything associated with it, but i guess not, atleast for the Excel object. Thanks again.
 
I had the same problem with a export to Excel 97. Tried the GC.Collect, and tried to kill the process, but no luck, the user could not open the file until my program was exited.

Finally opted to simply show the application. Then as an "value added" service, my program opened the Excel sheet which was just created for the user. My users thought this was very handy, and never realized it was a work around.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top