# Ruby and MS WORD

**URL:** <https://www.ruby-forum.com/t/ruby-and-ms-word/87397>\
**Category:** Ruby\
**Created:** [March 2, 2007, 8:48am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397 "2007-03-02T08:48:00Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 2, 2007, 8:48am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/1 "2007-03-02T08:48:00Z")

</div>

Hello!  
I’m new to Ruby and just wanted to ask if it had any means to create or  
edit MS Word documents. It so happened that I need to write a simple  
script filling templates with given data, but using C++ or Delphi is too  
much for that task I think. Besides, Ruby seems a bit prettier to me.

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 2, 2007, 8:51am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/2 "2007-03-02T08:51:45Z")

</div>

On 02/03/07, Ww Ee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello!  
> I’m new to Ruby and just wanted to ask if it had any means to create or  
> edit MS Word documents. It so happened that I need to write a simple  
> script filling templates with given data, but using C++ or Delphi is too  
> much for that task I think. Besides, Ruby seems a bit prettier to me.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Have a look at the Win32OLE libraries. Rather than creating MS Word  
files directly you can automate an instance of Word to do it for you.

Farrel

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 2, 2007, 9:08am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/3 "2007-03-02T09:08:27Z")

</div>

Farrel L. wrote:

> Have a look at the Win32OLE libraries. Rather than creating MS Word  
> files directly you can automate an instance of Word to do it for you.
> 
> Farrel

Thanks a lot! I wanted exactly something like that. Besides, I seriously  
doubt there’s a library which works with MS Word files directly.

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 2, 2007, 10:50am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/4 "2007-03-02T10:50:00Z")

</div>

From memory, there’s some good examples in the Ruby Garden wiki, but  
I’m having trouble loading the site (just the wiki, the rest is fine)  
currently.  
I’ve just got all the kids in bed am about to sit back and watch  
MI:III with my wife, but I’ll have another look later when we’re done.

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 2, 2007, 10:54am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/5 "2007-03-02T10:54:08Z")

</div>

Vlad E. wrote:

> Farrel L. wrote:
> 
> > Have a look at the Win32OLE libraries. Rather than creating MS Word  
> > files directly you can automate an instance of Word to do it for you.
> > 
> > Farrel
> 
> Thanks a lot! I wanted exactly something like that. Besides, I seriously  
> doubt there’s a library which works with MS Word files directly.

Hi Vlad,

Let me show you something regarding MSWord file format binding  
[http://jakarta.apache.org/poi/hwpf/index.html](http://jakarta.apache.org/poi/hwpf/index.html)

These guys are trying to make something like you want, but, for java  
platform.  
Anyway, the idea is that they named the project HWPF (Horrible Word  
Processor Format) 🙂  
Now I think you know why they are not so many bindings for this format,  
it’s a Horrible one. So, interacting with this kind of format, is not  
quite easy.

Sorry for ruby off-topic post.

All the best,

Alin

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 2, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/6 "2007-03-02T14:41:23Z")

</div>

Leslie V. schrieb:

> No, and as soon as you used it, the format would change anyway.  
> Information on how to do with with Win32OLE is in RubyGarden, but I  
> think it’s limited to Access, Outlook and Excel. Here’s an Excel  
> example from there:

More than ten years ago, when we wanted to programmatically create  
something like Word documents, we used the Rich Text Format (RTF). Word  
has no problems opening those documents. Maybe this is an option for the  
OP.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 2, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/7 "2007-03-02T12:42:14Z")

</div>

On 3/2/07, Vlad E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Farrel L. wrote:
> 
> > Have a look at the Win32OLE libraries. Rather than creating MS Word  
> > files directly you can automate an instance of Word to do it for you.
> > 
> > Farrel
> 
> Thanks a lot! I wanted exactly something like that. Besides, I seriously  
> doubt there’s a library which works with MS Word files directly.

No, and as soon as you used it, the format would change anyway.  
Information on how to do with with Win32OLE is in RubyGarden, but I  
think it’s limited to Access, Outlook and Excel. Here’s an Excel  
example from there:

---------------------------------------xx----------------------------------------------------  
excel = WIN32OLE::new(‘excel.Application’)  
workbook = excel.Workbooks.Open(‘c:\examples\spreadsheet.xls’)  
worksheet = workbook.Worksheets(1) #get hold of the first worksheet  
worksheet.Select #bring it to the front -need sometimes to run  
macros, not for working with a worksheet from ruby  
excel[‘Visible’] = true #make visible, set to false to make invisible  
again. Don’t need it to be visible for script to work

reading data from spreadsheet:

worksheet.Range(‘a12’)[‘Value’] #get value of single cell  
data = worksheet.Range(‘a1:c12’)[‘Value’] #read into 2D array

finding the first empty row (using empty column A)

line = ‘1’  
while worksheet.Range(“a#{line}”)[‘Value’]  
line.succ!  
end #line now holds row number of first empty row

or to read as you go

line = ‘1’  
data = []  
while worksheet.Range(“a#{line}”)[‘Value’]  
data \<\< worksheet.Range(“a#{line}:d#{line}”)[‘Value’]  
line.succ!  
end

writing data into spreadsheet, example

worksheet.Range(‘e2’)[‘Value’] = Time.now.strftime ‘%d/%m/%Y’ #single  
value  
worksheet.Range(‘a5:c5’)[‘Value’] = [‘Test’, ‘25’, ‘result’]

---------------------------------------xx----------------------------------------------------

As you can see, the VBA objects can be accessed pretty much directly.  
What you can do in Word is to record a macro to do what you want, and  
Word will create a VBA script for you. Edit the script (press Alt-F11)

- to see how Word did it, and then convert that to Ruby using the  
above as a guide.

Les

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 2, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/8 "2007-03-02T20:17:13Z")

</div>

On Fri, Mar 02, 2007 at 10:40:22PM +0900, Pit C. wrote:

> More than ten years ago, when we wanted to programmatically create  
> something like Word documents, we used the Rich Text Format (RTF). Word  
> has no problems opening those documents. Maybe this is an option for the OP.

It’s generally a better option, too – no possibility of Word macro  
viruses, smaller file sizes, et cetera. I’ve discovered, however, that  
many versions of MS Word will keep DOC format data in the file when  
translating to RTF, even though it’s unusable by the format, so that  
file size actually inflates and macros don’t go away. As such,  
translating to RTF isn’t as effective when using MS Word to do it as  
simply creating an RTF in the first place.

I wonder why nobody uses plain text any longer.

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 3, 2007, 7:10am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/9 "2007-03-03T07:10:15Z")

</div>

On 3/2/07, Ww Ee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> just wanted to ask if it had any means to create or edit MS Word documents

Yes. Basically you use the same VBA calls Office provides using Ruby  
through the Win32Ole library.

How do you find what methods or attributes to call/use?, there are  
several ways to find them:

- one is by using Word itself: go to Tools \> Macro \> Visual Basic  
Editor and once there go to View \> Object Browser, then go crazy and  
experiment with the gazillion options.
- Open a doc through IRB (see sample code below), and inspect objects  
by something like word\_object.ole\_methods (sadly you can’t .sort them)
- download and run a Ruby OLE browser, again go crazy. One can be  
found at [http://dave.burt.id.au/ruby/ole\_browser.rb](http://dave.burt.id.au/ruby/ole_browser.rb)

Now some Ruby:

require ‘win32ole’  
word = WIN32OLE.new(‘word.application’)  
word.visible = true  
word.documents.count

# open/create new document

word.documents.add

# or open file

word.documents.open(path\_to\_file)

# type something

word.selection.typetext(“Hello World!\n”)

# select whole text

word.selection.wholestory

# delete selection

word.selection.delete

# move to start of document

word.selection.start = 0  
word.selection.end = 0

# search for a string

word.selection.find.text = ‘my search’  
result = word.selection.find.execute

# read the selection

puts word.selection.text

# and position

puts word.selection.start  
puts word.selection.end

# or set the position, and selection

word.selection.start = 20  
word.selection.end = 23  
puts word.selection.text

# printing

word.options.printbackground = false  
word.activedocument.PrintOut

# SAVING document

word.activedocument.saveas file\_name, wdFormatText

# notice the 2nd parameter which is a numeric constant

# indicating the file format to save in thusly:

# I believe omitting 2nd parameter would save in native .doc)

# wdFormatDocument = 0 (no conversion)

# wdFormatTemplate = 1

# wdFormatText = 2

# wdFormatTextLineBreaks = 3

# wdFormatDOSText = 4

# wdFormatDOSTextLineBreaks = 5

# wdFormatRTF = 6

# wdFormatUnicodeText = 7 # it repeats!

# wdFormatEncodedText = 7

# wdFormatHTML = 8

# wdFormatWebArchive = 9

# wdFormatFilteredHTML = 10

# wdFormatXML = 11

# close document

word.activedocument.close( true ) # presents save dialog box  
word.activedocument.close( false ) # no save dialog, just close it

# quit word

word.quit

HIH  
–CT

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 7, 2007, 10:05am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/10 "2007-03-07T10:05:34Z")

</div>

Hi,

class WIN32OLE\_METHOD  
def \<=\>(other)  
self.to\_s \<=\> other.to\_s  
end  
end

and word.ole\_methods.sort work.

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 7, 2007, 10:10am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/11 "2007-03-07T10:10:35Z")

</div>

Another usefull one :

class WIN32OLE\_METHOD  
def to\_str  
self.to\_s  
end  
end

and then :

irb(main):032:0\> word.ole\_methods.grep(/Window/).sort  
=\> [ActiveWindow, NewWindow, ShowWindowsInTaskbar,  
ShowWindowsInTaskbar, WindowState,  
WindowState, Windows]

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [March 6, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/12 "2007-03-06T19:57:45Z")

</div>

Thak you very much! All of you. And a special thanks to carlos tirado!  
That helped a lot!

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [July 28, 2008, 11:04pm UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/13 "2008-07-28T23:04:50Z")

</div>

How do you open a document in read only mode??

carlos tirado wrote:

> # or open file
> 
> word.documents.open(path\_to\_file)

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [July 29, 2008, 4:13am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/14 "2008-07-29T04:13:16Z")

</div>

Nathan L. wrote:

> How do you open a document in read only mode??
> 
> carlos tirado wrote:
> 
> > # or open file
> > 
> > word.documents.open(path\_to\_file)

doc = word.documents.open(path\_to\_file, ‘ReadOnly’ =\> true)

David

> **[Ruby on Windows](http://rubyonwindows.blogspot.com)**
>
> On using the Ruby programming language on the Microsoft Windows platform

> **[Ruby on Windows](http://rubyonwindows.blogspot.com/search/label/word)**
>
> On using the Ruby programming language on the Microsoft Windows platform

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [July 29, 2008, 12:54am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/15 "2008-07-29T00:54:05Z")

</div>

Nathan L. wrote:

> How do you open a document in read only mode??

You may need to poke around the Microsoft developer site to see what the  
options are for that object. In the past, when I did all sorts of Word  
scripting, they were really good about providing the Office object model  
and APIs. (I found it very helpful to write code in VBA using the  
built-in IDE, then translating that to another language.)

–  
James B.

[www.happycamperstudios.com](http://www.happycamperstudios.com) - Wicked Cool Coding  
[www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [June 9, 2009, 6:45am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/16 "2009-06-09T06:45:01Z")

</div>

thanks… that was very helpful… in the same way is there any way to  
parse word documents with ruby…

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [June 8, 2009, 8:09pm UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/17 "2009-06-08T20:09:57Z")

</div>

that was so helpful… thx carlos… now can you help me in parsing the  
word documents…

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [June 9, 2009, 7:37am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/18 "2009-06-09T07:37:04Z")

</div>

Anandh K. wrote:

> thanks… that was very helpful… in the same way is there any way to  
> parse word documents with ruby…

you need to use win32ole. There are a few examples online.

Cheers,  
Mohit.  
6/9/2009 | 1:36 PM.

---

<div class="post-metadata">

**Author:** ![terix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/53a042/32.png) [@terix](https://www.ruby-forum.com/u/terix)\
**Post date:** [June 9, 2009, 8:30am UTC](https://www.ruby-forum.com/t/ruby-and-ms-word/87397/19 "2009-06-09T08:30:59Z")

</div>

On Jun 9, 6:45 am, Anandh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## thanks… that was very helpful… in the same way is there any way to parse word documents with ruby…
> 
> Posted viahttp://www.ruby-forum.com/.

I do not know what do you mean by “parse” but if you are accessing  
Word document through Office automation you should check  
[Microsoft Office 2007 | Microsoft Learn](http://msdn.microsoft.com/en-us/library/bb726436.aspx) where you can  
find documentation about Word Object Model with all classes and  
interfaces which you can use through win32ole in Ruby. You can also  
check my post about Outlook automation in Ruby at:  
[http://pragmaticdevnotes.wordpress.com/2008/12/17/with-a-little-help-from-ruby/](http://pragmaticdevnotes.wordpress.com/2008/12/17/with-a-little-help-from-ruby/).

Approach is quite similar so you only need to read Office automation  
reference and find exactly what you need.

Regards,  
Bosko
