djlewis wrote:
I’ve read all the forum stuff on getting the Excel process to
terminate. Yet the following code does not do that – Excel.exe
remains in task manager…
excel = WIN32OLE::new(‘excel.Application’)
ewb = excel.workbooks.add( t_path + “TTI-DB-Template.xls”)
ews = ewb.Worksheets.Item( 1)
ews.cells(1, 1).value = “sdfasd” # comment out to make it work
ewb.saveas( t_path + “Temptemp3.xls”)
not sure I need all this, but it can’t hurt (or can it?)
ewb.Close(0)
excel.Quit()
WIN32OLE.ole_free( excel)
WIN32OLE.ole_free( ewb)
WIN32OLE.ole_free( ews)
excel = nil
ews = nil
ewb = nil
GC.start
However, if I remove the line: ews.cells(1, 1).value = “sdfasd”, then
the Excel process DOES go away.
What’d going on here? How do I get rid of the Excel process if I put
anything into the worksheet/workbook?
Thanks. --David.
Your code runs fine on my machine, up through this point:
excel = WIN32OLE::new('excel.Application')
ewb = excel.workbooks.add( t_path + "TTI-DB-Template.xls")
ews = ewb.Worksheets.Item( 1)
ews.cells(1, 1).value = "sdfasd" # comment out to make it work
ewb.saveas( t_path + "Temptemp3.xls")
ewb.Close(0)
excel.Quit()
If I left out the remainder of your code, the Excel process terminated
properly.
If, however, I included the rest of your code verbatim…
WIN32OLE.ole_free( excel)
WIN32OLE.ole_free( ewb)
WIN32OLE.ole_free( ews)
excel = nil
ews = nil
ewb = nil
GC.start
…the Excel process did not terminate properly.
If I modified that code to this…
ews = nil
ewb = nil
excel = nil
WIN32OLE.ole_free( ews)
WIN32OLE.ole_free( ewb)
WIN32OLE.ole_free( excel)
GC.start
…the Excel process terminated properly.
My suggestions:
A) Set object variables to nil before calling ole_free.
B) Set object variables to nil in the reverse order that they were
created.
C) Don’t bother with nil, ole_free, and GC.start if you don’t need to.
Hope that helps.
David
http://rubyonwindows.blogspot.com
http://rubyonwindows.blogspot.com/search/label/excel