Sunday, January 24, 2010

Saving Microsoft Office Documents as PDFs

A recent discussion in the Ruby Forum reminded me that it is possible with Microsoft Office 2007 applications to save a document in Adobe PDF format.

In the Microsoft Word object model, you can call the Document object's SaveAs() method, passing it a filename, and the document will be saved in the default format.


document.SaveAs('c:\temp\MyDocument.doc')

But the SaveAs() method accepts an optional second parameter, an integer that specifies the file format. In the Word object model, the PDF format is represented by the value 17. So, where document represents your Document object, you can do the following:

document.SaveAs('c:\temp\MyDocument.pdf', 17)

In Microsoft Excel, the Workbook object's SaveAs() method accepts the value 57 to specify PDF format:

workbook.SaveAs('c:\temp\MyWorkbook.pdf', 57)

And in Microsoft PowerPoint, the magic number is 32:

presentation.SaveAs('c:\temp\MyPresentation.pdf', 32)

You can find enumerations for file types over at MSDN:

A tip of the hat to Joe Peck for mentioning this in the Ruby Forum.

Let me know if you have any questions or comments, and thanks for stopping by!

10 comments:

Lucas Prim said...

wow! really nice!
i´m guessing where to get the best documentation for win32ole stuff. can u help me?
good post!

John Allen said...

Hey David!
This is good stuff to know...but how can I find out what version of Word a user of any program I write with this info is using? If they have an older version of Office, I would like to be able to check before I offer the option of creating PDF files ;)
Looking forward to seeing the Book!

David Mullet said...

John-

The Word Application object includes a Version property that returns a string:

puts(word.Version)

With my copy of Word 2007, this returns "12.0", so you could do something like:

if word.Version.to_i >= 12

David

John Allen said...

Yeah...after I posted the question I went and figured it out. Thanks for the help :)

Paul said...

Where's the book David!!!!

We're all waiting!!!

wentbackward said...

In Word 2007 you also have more control over PDF options if you have microsofts "Save As PDF" installed:

def save_pdf(doc, filename)
doc.ExportAsFixedFormat(
"C:\mypdf.pdf", WdExportFormatPDF, false,
WdExportOptimizeForPrint, WdExportAllDocument, 1, 1,
WdExportDocumentContent, true, true,
WdExportCreateHeadingBookmarks,true,
true, false)
end

Anonymous said...

Hello!

I have with interest read all your Excel tips and tricks - very useful!

One question: I have clients using OpenOffice.org. Does OpenOffice.org implement the same OLE interfaces, or is it a completely new world to learn if I am to automate for instance the Calc module in OpenOffice.org instead of Excel?

Best regards
Stein-Erik

diva said...

wow you are good man..
but can i do that using open office??
isn't in different with ms.office??

Anonymous said...

When I try this I get:

WIN32OLERuntimeError: SaveAs
OLE error code:80004005 in

HRESULT error code:0x80020009
Exception occurred.
from (irb):5:in `method_missing'
from (irb):5

Any help would be appreciated!

Vasudev Ram said...

Nice useful post - thanks.
- Vasudev