[Ironruby-core] Interop with Excel

Tomas Matousek Tomas.Matousek at microsoft.com
Wed Apr 22 12:51:56 EDT 2009


This should now mostly work.

respond_to? is not supported for COM yet, but "methods" is (try workbooks.methods(false)).

require 'Microsoft.Office.Interop.Excel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'
include Microsoft::Office::Interop::Excel
excel = ApplicationClass.new
excel.Visible = true
workbooks = excel.Workbooks
workbooks.Open('c:\\foo.xsl')

You can also do:

def newCOM progId
  System::Activator.CreateInstance(System::Type.GetTypeFromProgID(progId))
end

ex = newCOM("Excel.Application")

ex.Visible = true
nb = ex.Workbooks.Add
ws = nb.Worksheets[1]
p ws.Name

10.times do |i| 
  10.times do |j| 
    ws.Cells[i + 1, j + 1] = (i + 1) * (j + 1)
  end
end

Tomas

-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Jeff Siebold
Sent: Wednesday, April 08, 2009 9:08 AM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Interop with Excel

I'm replying to my own post because it looks like I found part of my 
problem. I changed the code to:

require 'Microsoft.Office.Interop.Excel, Version=12.0.0.0, 
Culture=neutral, PublicKeyToken=71e9bce111e9429c'
include Microsoft::Office::Interop::Excel
excel = ApplicationClass.new
excel.Visible = true

and that works. But I'm still having trouble getting access a Workbook:

p excel.ActiveWorkbook
workbooks = excel.Workbooks
p workbooks.respond_to?('Open')

returns:
nil
false

I would be grateful for any help. Thanks, Jeff


Jeff Siebold wrote:
> The following code runs fine:
> 
>   require 'Microsoft.Office.Interop.Excel, Version=12.0.0.0,
> Culture=neutral, PublicKeyToken=71e9bce111e9429c'
>   Excel = Microsoft::Office::Interop::Excel
> 
> but when I try to get the Application object as follows:
> 
>   app = Excel.Application.new
> 
> I get the following error:
> 
> excel.rb:0: undefined method `Application' for
> Microsoft::Office::Interop::Excel:Module (NoMethodError)
> 
> am I not understanding how this works or is this just not working yet?

-- 
Posted via http://www.ruby-forum.com/.
_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core



More information about the Ironruby-core mailing list