Scripting Microsoft Word
(Page 1 of 4 )
Microsoft Word is a very powerful word processing application that is part of the Microsoft Office suite. It has become probably the single most widely used application of its type. But you already know this. What you may not know is that Microsoft Word was created with script automation in mind.
The developers at Microsoft have included an OLE automation object for Word that allows you to control it both internally with macros and externally with WSH. Today we’re going to explore this automation object and some of its uses.
So some of you may be thinking, “Why should I even care? I’m not going to automate typing a paper.” And you’re probably right. It would be far too tedious to automate typing a paper, but there are other possibilities.
Word is excellent for creating reports. You could create an automation that pulls information from a database, creates and formats a report, and then sends it off to a network printer. Wouldn’t it be nice to have overnight sales orders waiting on the printer when you arrive at the office each morning?
You already know that Microsoft Word can work very closely with an Access database for creating form letters. Why not automate the creation of personalized form letters and envelopes for your entire mailing list? And the possibilities go on.
Getting started is pretty simple. You first need to create an instance of the Word automation object. This will differ slightly depending upon what language you use, but here’s what it looks like in VBScript.
Set objWord = CreateObject("Word.Application")
“Word.Application” is a version independent ProgId that will connect to the OLE object with the most recent version number. So don’t worry if you have or have had multiple versions of Word installed; you’ll always be working with the most recent.
This basically launches the Microsoft Word application in memory. Let’s look at a couple of things we can do to control how the application opens.
objWord.DisplayAlerts = False
objWord.Visible = False
DisplayAlerts is a Boolean property that, when set to False, suppresses file type warnings when opening older version files. To avoid needing any outside user input, it’s a good idea to make sure that unnecessary message boxes are always suppressed.
Visible is a hidden property that accepts a Boolean value that determines whether or not the Word application is displayed on screen or opened in the background. Setting this value to True will make the application visible. This is sometimes useful during script creation and testing.
Next: Creating and Opening Documents >>
More Windows Scripting Articles
More By Nilpo