# Interop with Excel

**URL:** <https://www.ruby-forum.com/t/interop-with-excel/165041>\
**Category:** IronRuby\
**Created:** [April 8, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/interop-with-excel/165041 "2009-04-08T15:33:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [April 8, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/interop-with-excel/165041/1 "2009-04-08T15:33:38Z")

</div>

The following code runs fine:

require ‘Microsoft.Office.Interop.Excel, Version=12.0.0.0,  
Culture=neutral, PublicKeyToken=71e9bce111e9429c’  
Excel = Microsoft:🏢: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:🏢:Interop::Excel:Module (NoMethodError)

am I not understanding how this works or is this just not working yet?

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [April 8, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/interop-with-excel/165041/2 "2009-04-08T18:07:59Z")

</div>

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:🏢: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:🏢: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:🏢:Interop::Excel:Module (NoMethodError)
> 
> am I not understanding how this works or is this just not working yet?

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [April 8, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/interop-with-excel/165041/3 "2009-04-08T19:42:55Z")

</div>

When I do this, Excel opens up, but it opens up without a workbook. If I  
open a file manually (via Excel’s GUI), then the ActiveWorkbook is  
populated.

I don’t know the Excel API, but if excel.workbooks.open(“Foo.xls”) is  
supposed to open a workbook in the Excel instance, then you’ve found a  
bug. Excel.workbooks.open is being overridden by Ruby’s Kernel#open  
method.

If this is the case, can you open a bug at  
[http://rubyforge.org/tracker/?func=browse&group\_id=4359&atid=16798](http://rubyforge.org/tracker/?func=browse&group_id=4359&atid=16798) and  
assign it to Tomas?

Thanks,  
JD

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [April 8, 2009, 7:59pm UTC](https://www.ruby-forum.com/t/interop-with-excel/165041/4 "2009-04-08T19:59:53Z")

</div>

It’s arathe a missing feature. We don’t support COM yet.

Tomas

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [April 22, 2009, 7:54pm UTC](https://www.ruby-forum.com/t/interop-with-excel/165041/5 "2009-04-22T19:54:06Z")

</div>

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:🏢: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

---

<div class="post-metadata">

**Author:** ![sprsquish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sprsquish](https://www.ruby-forum.com/u/sprsquish)\
**Post date:** [April 22, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/interop-with-excel/165041/6 "2009-04-22T22:28:22Z")

</div>

The COM interop stuff looks great.  
Does this mean that there’s now a way to “cast” a COM object to a type  
(and  
perform a QueryInterface) ?
