# Trouble with WIN32OLE obj, methods that return other objects

**URL:** <https://www.ruby-forum.com/t/trouble-with-win32ole-obj-methods-that-return-other-objects/64024>\
**Category:** Ruby\
**Created:** [June 21, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/trouble-with-win32ole-obj-methods-that-return-other-objects/64024 "2006-06-21T13:39:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhollender](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mhollender](https://www.ruby-forum.com/u/mhollender)\
**Post date:** [June 21, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/trouble-with-win32ole-obj-methods-that-return-other-objects/64024/1 "2006-06-21T13:39:03Z")

</div>

Dear Friends,

Is is possible in Ruby to instantiate WIN32OLE objects that are returned  
by a method call on other WIN32OLE objects?

The situation is the following. There is a  
NsComCom2.CommunicationComponent  
OLE (COM) object (for communicating with a Navision ERP system). It has  
a CreateoutMessage (yes, misspelled, it’s Microsoft, so don’t be  
surprised) method that returns another OLE object (a message object). So  
I

So I do

cc2 = WIN32OLE.new(“NsComCom2.CommunicationComponent”)  
and it works fine.

now, I would try to

msg = cc2.CreateoutMessage(“Message queue://.\private$\NIF”)

and it does _something_ but it does not instantiate the right object in  
the msg variable, because the methods I’d expect msg to have are not  
available. Yes, I know there are some casing quirks, so I tried  
createoutMessage, createoutmessage, create\_out\_message, createOutMessage  
and whatever, but it never worked. mgs.class just returns WIN32OLE.

Any ideas?

An Excel example will just do fine - say, call a method on a Sheet that  
returns a Range object into a variable, and then call some method of the  
Range object instance.

---

<div class="post-metadata">

**Author:** ![mhollender](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mhollender](https://www.ruby-forum.com/u/mhollender)\
**Post date:** [June 21, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/trouble-with-win32ole-obj-methods-that-return-other-objects/64024/2 "2006-06-21T13:54:04Z")

</div>

Wow… an Excel example won’t do, as I methods returning other object  
work just fine with Excel, but not with these objects I use. But I think  
the objects are not buggy, because they work in VBScript, there must  
just be some special kind of behaviour.

My complete code is:

require ‘win32ole’

cc2 = WIN32OLE::new ‘NsComCom2.CommunicationComponent’  
mqba = WIN32OLE::new ‘MSMQBusAdapter.MSMQBusAdapter’  
cc2.AddBusAdapter mqba, 1  
mqba.OpenWriteQueue ‘.\private$\NIF’ , 1 , 0  
mqba.OpenReplyQueue ‘.\private$\NIF-REPLY’ , 0 , 0  
outMsg = cc2.CreateoutMessage(‘Message queue://.\private$\NIF’)  
outStr = outMsg.GetStream  
outStr.Write(“he”)  
outMsg.Send(0)

and I get an error stating outStr is a NilClass…

---

<div class="post-metadata">

**Author:** ![mhollender](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mhollender](https://www.ruby-forum.com/u/mhollender)\
**Post date:** [June 21, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/trouble-with-win32ole-obj-methods-that-return-other-objects/64024/3 "2006-06-21T18:01:37Z")

</div>

I have more focus on the problem now. It seems COM objects returned by a  
method call on another COM object CAN be instantiated in Ruby EXCEPT  
when it’s an IStream object. But why and how to work around it?
