Printing Documents in WSH - Printing Word and PDF Documents
(Page 5 of 7 )
There may still be a few instances where the Shell method and Notepad may not be enough for your printing needs. In these cases, you can look to outside applications. This may be especially true if you are trying to print a document in a proprietary format.
I’m going to show you very quickly how to automate two common applications for printing Microsoft Word and Adobe PDF documents. Again, if your files use registered file extensions, even these should be printable using the Shell method.
Microsoft Word supports a very wide range of document formats and can be a very powerful tool for printing. The Word OLE automation object also provides very thorough access to Word’s print capabilities, so printing with this method is both powerful and highly configurable.
Const wdDoNotSaveChanges = 0
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
objWord.DisplayAlerts = False
Begin using Word by connecting to its OLE automation object. I’m also setting its Visible property to False so that the application window is not visible, and its DisplayAlerts property to false to prevent displaying message boxes that require user input.
objWord.Documents.Open("mydoc.doc")
objWord.Options.PrintBackground = False
objWord.ActiveDocument.PrintOut
objWord.Application.Quit wdDoNotSaveChanges
From here, printing is pretty straightforward. Use the Open method to open a document and then use the Document object’s PrintOut method to send it to the default printer.
Setting the PrintBackground property to False disables Word’s background printing feature and will prevent errors if the Word object is released before the print job completes.
It should be noted that the Word object’s print capabilities are much more powerful than you see here. For a more in-depth look at printing with Microsoft Word, read my upcoming article “Advanced Word Object Scripting” here on ASP Free.
Now we’ll take a look at scripting Adobe Reader for printing PDF and other supported documents.
Set objAcroPDF = CreateObject("AcroPDF.PDF")
objAcroPDF.LoadFile(mypdf.pdf)
objAcroPDF.PrintAll
Set objAcroPDF = Nothing
Here we’re connecting to the AcroPDF object and using its LoadFile method to open a document. Then a call to its PrintAll method sends the document to the default printer.
I don’t think the AcroPDF object was intended for use in scripting outside of Adobe’s own macros. However, connecting to its COM object does expose methods to open and print documents. I have not found an intrinsic method for closing the application, so be sure to set its object reference to Nothing after printing in order to release the object.
The AcroPDF object also exposes several other print methods that allow you to control print ranges and whether or not the document should be shrunk to fit pages. It also provides a printWithDialog method that will display the Print dialog box and allow the user to select print settings.
objAcroPDF.printAllFit(blnShrinkToFit)
objAcroPDF.printPages(lngFrom, lngTo)
objAcroPDF.printPagesFit(lngFrom, lngTo, blnShrinkToFit)
objAcroPDF.printWithDialog
Next: Printing Web Documents >>
More Windows Scripting Articles
More By Nilpo