Ruby and MS WORD

W

Ww Ee

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.
 
F

Farrel Lifson

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.

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
 
V

Vlad E.

Farrel said:
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.
 
S

Sharon Phillips

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
 
A

Alin Popa

Vlad said:
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

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
 
L

Leslie Viljoen

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
 
P

Pit Capitain

Leslie said:
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
 
C

Chad Perrin

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.
 
C

carlos tirado

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

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
 
V

Vlad E.

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

come

Hi,

class WIN32OLE_METHOD
def <=>(other)
self.to_s <=> other.to_s
end
end

and word.ole_methods.sort work.
 
C

come

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]
 
J

James Britt

Nathan said:
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 Britt

www.happycamperstudios.com - Wicked Cool Coding
www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff
 
A

Anandh Kumar

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

Anandh Kumar

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

Mohit Sindhwani

Anandh said:
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.
 
B

Bosko Ivanisevic

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

I do not know what do you mean by "parse" but if you are accessing
Word document through Office automation you should check
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/.

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

Regards,
Bosko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,614
Members
45,292
Latest member
EttaCasill

Latest Threads

Top