In my last article, I showed you how to connect to Microsoft Word’s OLE automation object to open and create documents. You also learned how to edit and save them. In today’s article, I’m going to take things a bit further and show you some scripting techniques that can be used to perform some more advanced functions.
We’ll begin with one of the most popular requests I receive. I’m constantly being asked how to read text from a Word document. Well, you’re in luck because I’m going to show you how—and it’s actually pretty simple.
To get started, you’ll either need to open a document or create a new one and fill it with some text. You learned how to do both in my last article so I won’t go into too much detail.
Const wdDoNotSaveChanges = 0
strDocument = "C:mydoc.docx"
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
objWord.DisplayAlerts = False
objWord.Documents.Open strDocument,, True
Set objDoc = objWord.ActiveDocument
What I’ve done here is created an instance of the Word automation object, set some basic properties, and opened an existing Word document. In this example, I’ve opened the document as read-only to prevent accidentally saving any changes.
Set objRange = objDoc.Content
strContents = objWord.CleanString(objRange.Text)
Next, I set a range that includes the entire contents of the document using the Document object’s Content property. That Range object has a Text property that returns a text string containing the text of that range. I’ve gone a little further by using the Word object’s CleanString function as well. This function will remove any Word-specific formatting from the string.
objWord.Quit wdDoNotSaveChanges
MsgBox strContents
I finish up by closing the Word application and displaying the contents in a message box. Of course, you could use this text string in any way you like.
I also get a lot of readers asking how they can print Word documents from within a WSH script. Many of them are looking for ways to automate report printing or simply to perform batch printing of multiple documents. Whatever your specific need might be, this is a fairly simple task to perform.
Const wdDoNotSaveChanges = 0
strDocument = "C:mydoc.docx"
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
objWord.DisplayAlerts = False
objWord.Documents.Open strDocument,, True
Set objDoc = objWord.ActiveDocument
Once again, you’ll begin by connecting to the Word automation object and opening the document that you wish to print. Here again, I’m keeping the Word application hidden since most automation scripts are designed to run unattended.
objWord.Options.PrintBackground = False
Next, we’re going to turn off Word’s Background Printing feature. An error message is sometimes displayed if the Word object is released before the print job completes. Turning off Background Printing will eliminate this error.
objDoc.PrintOut
Finally, we get to the meat and potatoes of the script. We use the Document object's PrintOut method to send our document to the printer. Note that this method will print to the printer that is found in Word’s default printer setting. In most cases, this will be the last printer that was used within Word. If you would like to print to another printer, or just prevent some unwanted mishaps by specifying a printer, you can use the code below instead.
strActivePrinter = objWord.ActivePrinter
objWord.ActivePrinter = "HP LaserJet 4 local on LPT1:"
objDoc.PrintOut
objWord.ActivePrinter = strActivePrinter
This piece of code first polls the ActivePrinter property for its current value and assigns it to a variable. Then it sets that value manually before calling the PrintOut method. Finally, the ActivePrinter property is set back to its original value.
This does present one major caveat in some cases. Setting the value of the ActivePrinter property in Word will also set the system default printer. You may not wish to change the system’s default printer setting. In the next example, I’ll show you how to set a Word printer setting without changing the system default printer.
objWord.WordBasic.FilePrintSetup "HP LaserJet 4 local on LPT1:", 1
Here I’m using the FilePrintSetup method. This method allows me to specify an optional parameter that tells Word not to set the system default printer when changing its active printer.
objWord.Quit wdDoNotSaveChanges
Now we can close the document and exit the Word application.
If you’re performing batch print jobs, you may want to think of ways to that this functionality could be packaged in a reusable function.
Okay, so simply printing a document may not be enough for you. After all, what if you want to print multiple copies or just specific page ranges? Most of the settings available in Word’s Print… dialog are also available through the Word automation object.
In most cases, there are two different ways to specify these settings: as parameters to the PrintOut method or as property settings of either the Options object or the PageSetup object.
Select Properties of the Options object
PrintBackground
A Boolean value that indicates whether Word prints in the background.
PrintBackgrounds
A Boolean value that indicates whether background colors and images are printed.
PrintComments
True if Microsoft Word prints comments, starting on a new page at the end of the document. Read/write Boolean.
PrintDraft
True if Microsoft Word prints using minimal formatting. Read/write Boolean.
PrintDrawingObjects
True if Microsoft Word prints drawing objects. Read/write Boolean.
PrintEvenPagesInAscendingOrder
True if Microsoft Word prints even pages in ascending order during manual duplex printing. Read/write Boolean.
PrintFieldCodes
True if Microsoft Word prints field codes instead of field results. Read/write Boolean.
PrintHiddenText
True if hidden text is printed. Read/write Boolean.
PrintOddPagesInAscendingOrder
True if Microsoft Word prints odd pages in ascending order during manual duplex printing. Read/write Boolean.
PrintProperties
True if Microsoft Word prints document summary information on a separate page at the end of the document. Read/write Boolean.
PrintReverse
True if Microsoft Word prints pages in reverse order. Read/write Boolean.
PrintXMLTags
Returns a Boolean that represents whether to print the XML tags when printing a document. Corresponds to the XML tags check box on the Print tab in the Options dialog box.
You’ve already seen how we’ve used the PrintBackground property in the examples provided so far. Let’s look at a couple more possible implementations.
objWord.Options.PrintDraft = True
objWord.Options.PrintHiddenText = False
This code sample will enable Draft printing while instructing Word not to print hidden text.
True for Microsoft Word to print a document in a series of booklets so the printed pages can be folded and read as a book. Read/write Boolean.
BookFoldPrintingSheets
Returns or sets a Long which represents the number of pages for each booklet. Read/write Boolean.
BookFoldRevPrinting
True for Microsoft Word to reverse the printing order for book fold printing of bidirectional or Asian language documents. Read/write Boolean.
BottomMargin
Returns or sets the distance (in points) between the bottom edge of the page and the bottom boundary of the body text. Read/write Single.
DifferentFirstPageHeaderFooter
True if a different header or footer is used on the first page. Can be True, False, or wdUndefined. Read/write Long.
FirstPageTray
Returns or sets the paper tray to use for the first page of a document or section. Read/write WdPaperTray.
FooterDistance
Returns or sets the distance (in points) between the footer and the bottom of the page. Read/write Single.
Gutter
Returns or sets the amount (in points) of extra margin space added to each page in a document or section for binding. Read/write Single.
GutterPos
Returns or sets on which side the gutter appears in a document. Read/write WdGutterStyle.
GutterStyle
Returns or sets whether Microsoft Word uses gutters for the current document based on a right-to-left language or a left-to-right language. Read/write WdGutterStyleOld.
HeaderDistance
Returns or sets the distance (in points) between the header and the top of the page. Read/write Single.
LayoutMode
Returns or sets the layout mode for the current document. Read/write WdLayoutMode.
LeftMargin
Returns or sets the distance (in points) between the left edge of the page and the left boundary of the body text. Read/write Single.
Orientation
Returns or sets the orientation of the page. Read/write WdOrientation.
OtherPagesTray
Returns or sets the paper tray to be used for all but the first page of a document or section. Read/write WdPaperTray.
PageHeight
Returns or sets the height of the page in points. Read/write Single.
PageWidth
Returns or sets the width of the page in points. Read/write Single.
PaperSize
Returns or sets the paper size. Read/write WdPaperSize.
RightMargin
Returns or sets the distance (in points) between the right edge of the page and the right boundary of the body text. Read/write Single.
TopMargin
Returns or sets the distance (in points) between the top edge of the page and the top boundary of the body text. Read/write Single.
TwoPagesOnOne
True if Microsoft Word prints the specified document two pages per sheet. Read/write Boolean.
VerticalAlignment
Returns or sets the vertical alignment of text on each page in a document or section. Read/write WdVerticalAlignment.
The PageSetup object provides access to the settings in Word’s Page Setup… dialog box. These settings are used to control page layouts, margins, and more. They can be used in conjunction with the Options object’s properties to create different print effects.
In this example, I’m manually setting the left and right margins to one inch (72 points) and changing the layout to use legal size paper. For more information on the PageSetup object’s properties and associated constant values, refer to the MSDN documentation here.
The PageSetup object can be used to set properties for the entire document, specific pages, or even specific sections!
Finally, you’re probably wondering about printing specific pages or page ranges. These types of settings must be supplied as parameters to the PrintOut method. There are many different optional parameters available. I’m not going to cover all of them here, but I’ll show you a couple of common examples.
Both of these examples perform the same task by setting different property values. The idea here is to print four total pages: page 2, and pages 4 through 6. The first example uses a print range. In the third parameter I’ve specified that I’m using a From-To range. Then, in the fifth and sixth parameters I’ve specified the starting and ending pages, respectively. Finally, I use the ninth parameter to specify a single addition page to print—page 2.
The value of the ninth parameter (or the “Pages” parameter) of the PrintOut method must always be enclosed in quotes. This is to avoid conflicts whenever you supply multiple ranges separated by commas.
In the second example, I’ve done this again, but I’m only specifying the ninth parameter. It accepts any comma-separated combination of individual page numbers and ranges.
In my final example, I’m telling Word to print ten copies of my current document and to collate them on the printer. I’m using the eighth parameter to specify the number of copies to print, and a Boolean value in the twelfth parameter turns collation on or off.
The syntax of the PrintOut method is a bit complicated. For more information, see the MSDN documentation here. You can mix and match a combination of any of the methods I’ve shown you here to achieve the desired result.
You’ve now seen how you can read and print the contents of your Word documents using Word’s OLE automation object. Take the time to explore the possibilities. The Word object is very detailed and offers much more power than I could capture in a few articles. Don’t be afraid to leaf through the MSDN’s online documentation. Until next time, keep coding!