4

I know this question has been asked when using VB directly, but I am having trouble terminating a process. I'm using the jruby version of win32ole and ruby 1.9. My current code is something like this:

begin
 excel = WIN32OLE::connect('excel.Application')
rescue
 excel = WIN32OLE::new('excel.Application')
end
excel.Visible = 1
workbook = excel.Workbooks.Open("path to some doc")
ws = workbook.Worksheets(1)
ws.Select
cell_content = ws.Cells(4,4).Value
puts("#{cell_content}")
workbook.Close
cell_content = nil
ws = nil
workbook = nil
excel.ole_free
excel = nil
GC.start

I'm trying to simply open excel, get a value and terminate. But when I look up task manager, I still see the EXCEL.exe running. Due to the begin-rescue it should also open an existing exe but every time I run it, it starts a new process. Once the script stops I manually close excel but I have also tried using excel.Close. Do I need to be calling the ruby equivalent of Marshal.releaseComObject? I don't have a lot of VB experience and I'm pretty new to ruby so I'm sorry if this is a dumb question, but thanks in advance!

Edit: I have also tried using excel.quit and excel.Quit, sorry I forgot to mention that. Neither seem to be working.

Edit 2: I'm running on rubymine and using windows xp, using gem: jruby-win32ole

Edit 3: Code properly terminate process on another computer, so possibly a problem with versions? Worked on jruby 1.5.6, process hangs on 1.6.5.1

janDro
  • 1,426
  • 2
  • 11
  • 24

2 Answers2

3

Just hit the same issue, and found a neat solution that I thought I'd document here for the next time someone hits this problem (as excel.Quit does not work if you are calling #exit in your JRuby code)...

You can use the following one-liner in your JRuby code to release all objects allocated on the current thread:

Java::OrgRacobCom::ComThread.Release

You should use this line after you have finished with all your WIN32OLE objects, and before you call #exit.

You should only need to do this if you are calling #exit in your JRuby code, since a normal exit (by reaching the end of your code) does seem to release the COM objects.

The solution came to me after reading about the life-cycle of COM objects in Jacob/Racob, as documented here: JacobComLifetime

Adam
  • 51
  • 3
0

Try excel.Quit, which should close Excel.

Daiku
  • 1,237
  • 11
  • 20
  • thanks for the input, but it still does not terminate the program – janDro Jun 24 '13 at 15:49
  • You can still see the program in the taskbar, or you can see the process in task manager? – Daiku Jun 24 '13 at 16:03
  • The process in task manager. When I run the script multiple times I get a new process and I have to end up manually ending multiple processes from task manager. Sorry for the confusion, meant process not program – janDro Jun 24 '13 at 16:26