Automating DOC to PDF Conversion

The COM object model exposed by Microsoft Word gives us the power to save any document which Word can recognize into a PDF file (as long as you've downloaded and installed the XPS and PDF exporter first). And it turns out that programmatically converting between a document and a PDF file is a pretty simple task. Given 5 lines of script code, we can open a document and save it as something else. And below is a JavaScript sample that does just that:

 

toPdf.js

var msword = WScript.CreateObject("Word.Application");

msword.Visible = false;

var doc = msword.Documents.Open("C:\\sample.docx");

doc.SaveAs("C:\\sample.pdf", 17); // WdSaveFormat::wdFormatPDF

msword.quit();

 

If you want to browse the entire COM object model exposed by Word, you can open up MSWORD.OLB (which is where the type library exists in Office 2007) in a COM object viewer. Though note that if you are using oleview.exe that comes with Visual Studio, you will need to give the OLB file an EXE extension; apparently oleview has issues with oddly named files.

MSDN References

Word.Application

Word.Document

WdSaveFormat enumeration