Skip to content

Instantly share code, notes, and snippets.

@itamar
Created October 9, 2012 16:34
Show Gist options
  • Select an option

  • Save itamar/3859921 to your computer and use it in GitHub Desktop.

Select an option

Save itamar/3859921 to your computer and use it in GitHub Desktop.
using OLE32 thing for word export
Re: Rails and MS Word
Posted by Paul Corcoran (Guest)
on 2006-12-29 18:24
(Received via mailing list)
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.
-Paul
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment