[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby and MS WORD

Ww Ee

3/2/2007 7:48:00 AM

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

18 Answers

Farrel Lifson

3/2/2007 7:51:00 AM

0

On 02/03/07, Ww Ee <poxvuibr@mail.ru> 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-....
>
>

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

Ww Ee

3/2/2007 8:08:00 AM

0

Farrel Lifson 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.

--
Posted via http://www.ruby-....

David and Sharon Phillips

3/2/2007 9:50:00 AM

0

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

On 02/03/2007, at 7:08 PM, Vlad E. wrote:

> Farrel Lifson 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.
>
> --
> Posted via http://www.ruby-....
>


Alin Popa

3/2/2007 9:54:00 AM

0

Vlad E. wrote:
> Farrel Lifson 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/...

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

--
Posted via http://www.ruby-....

Leslie Viljoen

3/2/2007 11:41:00 AM

0

On 3/2/07, Vlad E. <poxvuibr@mail.ru> wrote:
> Farrel Lifson 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

Pit Capitain

3/2/2007 1:40:00 PM

0

Leslie Viljoen schrieb:
> On 3/2/07, Vlad E. <poxvuibr@mail.ru> wrote:
>> Farrel Lifson 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:

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

Chad Perrin

3/2/2007 7:16:00 PM

0

On Fri, Mar 02, 2007 at 10:40:22PM +0900, Pit Capitain 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.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.

tirado.carlos

3/3/2007 6:09:00 AM

0

On 3/2/07, Ww Ee <poxvuibr@mail.ru> 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_...

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

Ww Ee

3/6/2007 6:58:00 PM

0

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

--
Posted via http://www.ruby-....

come

3/7/2007 9:04:00 AM

0

Hi,

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

and word.ole_methods.sort work.

On 3 mar, 07:09, "carlos tirado" <tirado.car...@gmail.com> wrote:

> + 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)