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