Tk with win32ole in XP

The attached example rbw file will correctly execute “moose” from inside
Tk on Windows 7 with Ruby 1.9.3p125, with Office 2010 installed.
However, on Windows XP, with everything else the same, it fails.
The win32ole command works fine when called outside of Tk on either
version of Windows, but when called as in the attached, it fails with
the error “CoInitialize has not been called”.
I have confirmed that this works on 6 machines with Windows 7 (4 using
Ocra), and fails on 2 machines with XP (1 using Ocra).

My guess is that it’s not Tk itself, but that fact that win32ole is
being used by a thread other than the main script.

My question, in short, is how can I write a Tk application which is
capable of opening excel in both Windows 7 and XP?

Thanks,

Joe

My guess is that it’s not Tk itself, but that fact that win32ole is
being used by a thread other than the main script.

https://groups.google.com/forum/?fromgroups=#!topic/rubyinstaller/zaa_haj2KYA

may be related…

Roger, you are a god in human form!
This worked on both XP and Windows 7:


require ‘tk’
require “win32ole”
require ‘Win32API’
CoInitialize = Win32API.new(‘ole32’, ‘CoInitialize’, ‘P’, ‘L’)

def moose

filename = Dir.pwd + “\ReportOutput.txt”
CoInitialize.call( 0 )
excel = WIN32OLE::new(“excel.application”)
excel.visible = true
excel.Workbooks.Open(filename)

end

root = TkRoot.new
btn_OK = TkButton.new(root) do
text “OK”
borderwidth 5
underline 0
state “normal”
cursor “watch”
font TkFont.new(‘times 20 bold’)
foreground “red”
activebackground “blue”
relief “groove”
command (proc {moose})
pack(“side” => “right”, “padx”=> “50”, “pady”=> “10”)
end

Tk.mainloop