Rails and MS Word

Here is an example of creating a Word and Excel file from a Ruby application. Win32ole is bundled with Ruby 1.8 and above so no need to install it if your current on Ruby.


require 'win32ole'

#Excel OLE Automation example excel = WIN32OLE.new('Excel.Application') excel.visible = FALSE #Use TRUE to do this in the foreground with a window workbook = excel.Workbooks.Add(); worksheet = workbook.Worksheets(1); worksheet.Range('A1:E1').value = ['This','was','created','in', 'Ruby']; excel.ActiveWorkbook.SaveAs("c:\\CreatedFromRuby.xls") excel.Quit

#Word OLE Automation example word = WIN32OLE.new('Word.Application') word.visible = FALSE #Use TRUE to do this in the foreground with a window word.Documents.Add word.Selection.TypeText "This is some text." word.Selection.TypeParagraph word.Selection.BoldRun word.Selection.TypeText "This is some bold text.\n" word.Selection.TypeParagraph word.Selection.Font.Name = "Tahoma" word.Selection.BoldRun #toggle bold off word.Selection.TypeText "This is some text in the Tahoma font." word.ActiveDocument.SaveAs("c:\\CreatedFromRuby.doc") word.Quit

#For more information see MSDN Library/Office Solutions Development/Microsoft Office 2003/Office 2003/VBA Language Reference and then Excel or Word reference sections