# How to open a file with win32ole

**URL:** <https://www.ruby-forum.com/t/how-to-open-a-file-with-win32ole/150829>\
**Category:** Ruby\
**Created:** [October 13, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/how-to-open-a-file-with-win32ole/150829 "2008-10-13T21:48:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 13, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/how-to-open-a-file-with-win32ole/150829/1 "2008-10-13T21:48:19Z")

</div>

Hi all,

I want to open a file via win32ole. I find the method ‘Open’ there but  
always get error feedback. I wonder if anyone can give me a hand.

Thanks,

Li

########################  
require ‘win32ole’  
fs=WIN32OLE.new(‘SAPI.SpFileStream’)  
count=1  
fs.ole\_methods.each{|m|  
print count,"\t"  
print m,"\t"  
print m.return\_type\_detail,"\t"  
puts  
count+=1  
}

file=‘c:/test.txt’  
fs.open(file)###line14 here

win32ole2.rb:14:in `method\_missing’: open (WIN32OLERuntimeError)  
OLE error code:800C0005 in   
  
HRESULT error code:0x80020009  
Exception occurred. from win32ole2.rb:14

> Exit code: 1

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 14, 2008, 3:25am UTC](https://www.ruby-forum.com/t/how-to-open-a-file-with-win32ole/150829/2 "2008-10-14T03:25:45Z")

</div>

try this:

require ‘win32ole’  
fs=WIN32OLE.new(‘SAPI.SpFileStream’)

p fs.ole\_method(“open”).params

file=‘c:/test.txt’

fs.Open(file,2) #least two params  
fs.write(“data”)  
fs.Close

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 14, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/how-to-open-a-file-with-win32ole/150829/3 "2008-10-14T16:53:16Z")

</div>

gpy good wrote:

> try this:
> 
> require ‘win32ole’  
> fs=WIN32OLE.new(‘SAPI.SpFileStream’)
> 
> p fs.ole\_method(“open”).params
> 
> file=‘c:/test.txt’
> 
> fs.Open(file,2) #least two params  
> fs.write(“data”)  
> fs.Close

Hi bud,

Thank you very much for the tips. It is so sweet.

From this example I guess that if a method is on the list but the object  
cannot receive it, most likely that more parameters need to be passed  
in.

One more question: How am I supposed that I should pass number 2 in the  
script above, instead of other numbers like, 0, 1, 10,…?

Li
