Rails and MS Word

Hi,

I would like to generate a Microsoft Word document from within rails,
but I haven’t found a way to do it. Does anybody know how to do it?
Thanks.

Kind regards,

Nick

You have to make a different component independent of Rails which
would parse XSL-FO stylesheets.

Thanks for the answer. But isn’t their an easier way to work with Word.
Doesn’t anybody have to generate some Word documents from its
application? Thanks.

Kind regards,

Nick

MS-Word can write and read a standard XML document schema. For MS-Word
2003, Microsoft published this schema:

http://tinyurl.com/6qckp

Trying to mimic this to write a document from scratch would be quite a
task. I think a better approach would be to mock-up the document you
want to output in MS-Word, then do a save-as-XML. Examine that XML
document in a text editor, and have your program output the same schema,
with your data of course. The result will be an XML document that
someone could open in MS-Word just by double-clicking.

Below is a simple “Hello World!” document after save-as-xml. As you can
see, it’s pretty complex, but you could certainly make this happen using
Builder without too much trouble.

I think the XML output functionality extends back to Office 2000. For
maximum compatibility, you’ll want to use the older XML output if you
can get your hands on it.

c.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?mso-application progid="Word.Document"?>

<w:wordDocument
xmlns:w=“http://schemas.microsoft.com/office/word/2003/wordml
xmlns:v=“urn:schemas-microsoft-com:vml”
xmlns:w10=“urn:schemas-microsoft-com:office:word”
xmlns:sl=“http://schemas.microsoft.com/schemaLibrary/2003/core
xmlns:aml=“http://schemas.microsoft.com/aml/2001/core
xmlns:wx=“http://schemas.microsoft.com/office/word/2003/auxHint
xmlns:o=“urn:schemas-microsoft-com:office:office”
xmlns:dt=“uuid:C2F41010-65B3-11d1-A29F-00AA00C14882”
w:macrosPresent=“no” w:embeddedObjPresent=“no” w:ocxPresent=“no”
xml:space=“preserve”><o:DocumentProperties><o:Title>Hello
World</o:Title><o:Author>Network
Services</o:Author><o:LastAuthor>Network
Services</o:LastAuthor><o:Revision>1</o:Revision><o:TotalTime>0</o:TotalTime><o:Created>2006-10-16T12:35:00Z</o:Created><o:LastSaved>2006-10-16T12:35:00Z</o:LastSaved><o:Pages>1</o:Pages><o:Words>1</o:Words><o:Characters>12</o:Characters><o:Company>Some
Company</o:Company><o:Lines>1</o:Lines><o:Paragraphs>1</o:Paragraphs><o:CharactersWithSpaces>12</o:CharactersWithSpaces><o:Version>11.6568</o:Version></o:DocumentProperties><w:fonts><w:defaultFonts
w:ascii=“Times New Roman” w:fareast=“Times New Roman” w:h-ansi=“Times
New Roman” w:cs=“Times New
Roman”/></w:fonts><w:styles><w:versionOfBuiltInStylenames
w:val=“4”/><w:latentStyles w:defLockedState=“off”
w:latentStyleCount=“156”/><w:style w:type=“paragraph” w:default=“on”
w:styleId=“Normal”><w:name w:val=“Normal”/><w:rPr><wx:font wx:val=“Times
New Roman”/><w:sz w:val=“24”/><w:sz-cs w:val=“24”/><w:lang w:val=“EN-US”
w:fareast=“EN-US” w:bidi=“AR-SA”/></w:rPr></w:style><w:style
w:type=“character” w:default=“on”
w:styleId=“DefaultParagraphFont”><w:name w:val=“Default Paragraph
Font”/><w:semiHidden/></w:style><w:style w:type=“table” w:default=“on”
w:styleId=“TableNormal”><w:name w:val=“Normal Table”/><wx:uiName
wx:val=“Table Normal”/><w:semiHidden/><w:rPr><wx:font wx:val=“Times New
Roman”/></w:rPr><w:tblPr><w:tblInd w:w=“0”
w:type=“dxa”/><w:tblCellMar><w:top w:w=“0” w:type=“dxa”/><w:left
w:w=“108” w:type=“dxa”/><w:bottom w:w=“0” w:type=“dxa”/><w:right
w:w=“108” w:type=“dxa”/></w:tblCellMar></w:tblPr></w:style><w:style
w:type=“list” w:default=“on” w:styleId=“NoList”><w:name w:val=“No
List”/><w:semiHidden/></w:style></w:styles><w:shapeDefaults><o:shapedefaults
v:ext=“edit” spidmax=“2050”/><o:shapelayout v:ext=“edit”><o:idmap
v:ext=“edit”
data=“1”/></o:shapelayout></w:shapeDefaults><w:docPr><w:view
w:val=“print”/><w:zoom
w:percent=“100”/><w:doNotEmbedSystemFonts/><w:proofState
w:spelling=“clean” w:grammar=“clean”/><w:attachedTemplate
w:val=“”/><w:defaultTabStop w:val=“720”/><w:characterSpacingControl
w:val=“DontCompress”/><w:optimizeForBrowser/><w:validateAgainstSchema/><w:saveInvalidXML
w:val=“off”/><w:ignoreMixedContent
w:val=“off”/><w:alwaysShowPlaceholderText
w:val=“off”/><w:compat><w:dontAllowFieldEndSelect/><w:useWord2002TableStyleRules/></w:compat></w:docPr><w:body>wx:sect<w:p><w:r><w:t>Hello
World!</w:t></w:r></w:p><w:sectPr><w:pgSz w:w=“12240”
w:h=“15840”/><w:pgMar w:top=“1440” w:right=“1800” w:bottom=“1440”
w:left=“1800” w:header=“720” w:footer=“720” w:gutter=“0”/><w:cols
w:space=“720”/><w:docGrid
w:line-pitch=“360”/></w:sectPr></wx:sect></w:body></w:wordDocument>

Nick S. wrote:

Thanks for the answer. But isn’t their an easier way to work with Word.
Doesn’t anybody have to generate some Word documents from its
application? Thanks.

Kind regards,

Nick

On Mon, 2006-10-16 at 08:12, Nick S. wrote:

Thanks for the answer. But isn’t their an easier way to work with Word.
Doesn’t anybody have to generate some Word documents from its
application? Thanks.

I don’t think that there’s any easy way to work with Word.

I’ve done this sort of thing using OpenOffice from Rails- it’s fairly
simple to work with. The easiest way for you might be to create/edit an
OpenOffice document and then convert it to a Word document from
OpenOffice. I haven’t checked, but you might also be able to automate
the conversion.

George


Sterling Web Services
http://www.websterling.com
The Web Done Right

Hi George,

did you use a particular library to interface with OpenOffice. How do
the guys with PHP achieve to work with Word?

Kind regards,

Nick

You may just want to look into using .rtf’s, they are ALOT easier to
handle/modify and most likely will open straight in word.

On Oct 16, 8:45 am, Cayce B. [email protected]
wrote:

someone could open in MS-Word just by double-clicking.

xmlns:dt=“uuid:C2F41010-65B3-11d1-A29F-00AA00C14882”
w:latentStyleCount=“156”/><w:style w:type=“paragraph” w:default=“on”
w:w=“108” w:type=“dxa”/><w:bottom w:w=“0” w:type=“dxa”/><w:right
w:val=“DontCompress”/><w:optimizeForBrowser/><w:validateAgainstSchema/><w:saveInvalidXML

Thanks for the answer. But isn’t their an easier way to work with Word.
Doesn’t anybody have to generate some Word documents from its
application? Thanks.

Kind regards,

Nick–
Posted viahttp://www.ruby-forum.com/.

It might also make sense to output an RTF file. There is a Ruby RTF
project on Rubyforge, but I’ve never used it and it looks like it
hasn’t been updated in a year or so. YMMV.

_Kevin

If your Rails app is running on Windows-based servers and you’re
comfortable with having Word itself installed on the server, you can use
ruby Win32::OLE libraries to make calls to Word like youn would from
other languages.

If your app is running on a Unix-family OS, then your choices are as
others have pretty much described. Depending on how complex a document
you’re trying to generate and how much of it is custom, different
approaches will be better for different circumstances. I haven’t tried
it, but judging by the docs, the Ruby RTF library looks like the best
bet to me for generating documents from scratch, especially if you’re
not already an OLE programmer.

Nick S. wrote:

Hi,

I would like to generate a Microsoft Word document from within rails,
but I haven’t found a way to do it. Does anybody know how to do it?
Thanks.

Kind regards,

Nick

George wrote:

I’ve done this sort of thing using OpenOffice from Rails- it’s fairly
simple to work with.

I have been looking at serving up an OpenOffice doc but could not
decide on the best approach. eg. I have an order form which is
created as part of a user process and then emailed as confirmation and
printed from the browser. It would be nice if the user could also ask
for a copy of the form to be served into openoffice (without too many
itermediate steps) I guess some of the answer is probably more
applicable to an openoffice forum, but from a rails point of view, I
cant think how to make the page available from the web page interface -
it’s something i have never seen described. Maybe it cant be done,
other than as an upload?

Can anyone offer any pointers?

tonypm

I currently need the same funcionality for an app, and I’m planning to
do it with ASP.NET and then, if I want to extend the application, I will
use webservices to connect it to the rest of the extension (that will be
in ror, of course).

What do you think about that? to use the simplest tools for each task
and then connect them using webservices?

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