In my last two articles I’ve demonstrated ways of using Microsoft Word through its OLE automation object. In this article, I’m going to show you a few more advanced scripting techniques that you can use. The first two are best used for data mining or mass-file editing while the last is a way of exploiting some of Word’s functionality for use in other environments.
We’ll begin by taking a look at Word’s Find and Replace features. These features allow you to search a Word document for words or phrases and replace them. You’ve more than likely used these features within the Word application so I’m not going to go into any more detail describing them. Instead, let’s learn how to script them!
For now, we’ll focus on the Find feature. This can be useful for several different purposes. Perhaps you want to mine some data from a Word document. The Find feature would allow you to find that data quickly and efficiently. Or maybe you have a folder full of documents and you're looking for a quick way to identify which ones pertain to a specific topic. Whatever your purpose might be, scripting the Find feature could prove to be useful.
Const wdFindContinue = 1
strFindText = "Text to find"
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set objDoc = objWord.Documents.Open("mydoc.doc")
As we’ve done before, we’ll begin our script by setting a few constants and variables, creating an instance of the Word application, and opening a document. The strFindText variable should contain the text string that you wish to find in your Word document.
Set objSelection = objWord.Selection
Next we’ll want to create a selection containing the entire contents of the Word document. This allows us to search the entire document. Want to limit your search to a specific part of the document? That’s fine too. In that case, set your selection to only the parts of the document you wish to search. This could be a single page or even a single paragraph.
Finally, we actually perform the Find function. I’ve set several different properties to define how my search should be completed before using the Execute method to set it in motion. Once the search is completed, the Found property will return the number of instances where my search text was found within the document.
The Execute method returns a Boolean value based on whether or not the Find operation returned any matches.
By setting the MatchWholeWord property to true, I’m telling Word that my search text should only match whole words and should not be found within words. For example, the search term “car” will not match the word “cardinal.”
The MatchAllWordForms property gives us a way to add a little flexibility to our search. It matches all forms of a word. For example, if your search term is “fly,” setting this value to True will match the words “flies” and “flying” as well. This property is set to false by default.
The MatchCase property accepts a Boolean value that indicates whether Word should perform a case-sensitive search of the supplied term.
The Wrap property is a Boolean value that determines what happens if the search is started anywhere other than the beginning of the document and the end of the document is reached. With this value set to True, Word will go back to the beginning of the document and continue searching until it reaches the point where it started.
The Forward property controls the direction in which Word searches. Setting this value to True will search forward through the document while False will search backwards. This value is True by default.
Finally, the Text property is used to supply the search term or phrase to be found. There are other properties available as well that control how the search is performed. For a full list of these properties, consult the MSDN documentation here.
In many cases, finding may not be enough. The next logical step is to have the ability to change what you find. Microsoft Word provides a Replace function for this. We are able to access that feature through Word’s OLE automation object as well.
The construct is similar to what we used to conduct a search using the Find feature.
Const wdDoNotSaveChanges = 0
Const wdFindContinue = 1
Const wdReplaceAll = 2
strFindText = "Text to find"
strReplaceText = "Text to replace with"
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set objDoc = objWord.Documents.Open("mydoc.doc")
Set objSelection = objWord.Selection
With objSelection.Find
.MatchWholeWord = vbTrue
.MatchCase = vbFalse
.Wrap = wdFindContinue
.Forward = vbTrue
.Text = strFindText
.Replacement.Text = strReplaceText
blnFound = .Execute(,,,,,,,,,, wdReplaceAll)
If blnFound Then
intMatches = .Found
Else
intMatches = 0
End If
End With
The Replace feature is actually an extension of the Find feature, so we’re going to use the same object with some added properties.
The Find object has a Replacement sub-object that represents the Replacements made using the Find/Replace feature. This Replace object has a Text property that must be set to the text with which you wish your search terms to be replaced.
Finally, the only other difference is the addition of a parameter when calling the Execute method. By supplying the eleventh parameter with an integer value of 2, we’re telling Word to replace all instances. You may also supply wdReplaceOne (1) to replace only the first instance. This parameter default is to wdReplaceNone (0) which instructs Word to perform the search only without making any replacements.
objDoc.Close
objWord.Quit
WScript.Echo intMatches, "instances were replaced."
With all of your replacements made, you can close your document and exit the Word application.
As I stated in the beginning, this last use of the Word object will allow you to exploit some of Word’s functionality for use outside of the Word application. In this example, I’m going to show you how to access Word’s built-in spelling dictionary to use Word as a spell checker.
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set objDoc = objWord.Documents.Add
strWord = "mispelled"
We’ll begin by creating an instance of the Word automation object and then creating a new blank document. We’re not actually going to use the document, but this allows us to access Word’s spell checking feature. We’re also supplying a word to spell-check.
Next, we use the Word object’s CheckSpelling method to do the work. This method returns a Boolean value indicating whether or not the word was spelled correctly -- or, more accurately, whether or not the word was found in Word’s dictionary.
If blnSpelledCorrectly Then
WScript.Echo strWord & " is spelled correctly."
Else
WScript.Echo strWord & " is spelled incorrectly."
End If
As you can see, using Word’s spell checking feature is actually pretty easy. But we can take this a bit further. As you know, when Word finds an incorrectly spelled word, it will kindly offer possible suggestions. As it turns out, you can get those in your script as well.
If blnSpelledCorrectly Then
WScript.Echo strWord & " is spelled correctly."
Else
Set colSuggestions = objWord.GetSpellingSuggestions(strWord)
WScript.Echo strWord & " is spelled incorrectly. You might try:"
For Each strSuggestion in colSuggestions
Wscript.Echo strSuggestion
Next
End If
When a word is spelled incorrectly, you can use the Word object’s GetSpellingSuggestions method to retrieve Word’s suggested spellings. This method returns a collection object containing each of the suggestions.
Of course, since scripting is intended for some level of automation, it’s very possible that you may not want to return all of the suggestions, since there is a good possibility there won’t be anyone sitting at the computer to choose one. In this case, you may wish instead to return only the first, most relevant result.
If blnSpelledCorrectly Then
WScript.Echo strWord & " is spelled correctly."
Else
Set colSuggestions = objWord.GetSpellingSuggestions("strWord")
WScript.Echo strWord & " should be spelled " & colSuggestions(0) & "."
End If
Here, instead of iterating through each of the possible suggestions, I’m simply grabbing the first element in the collection. This is mostly effective since Word returns the most closely matched possibility first.
objWord.Quit
When you’re finished using Word’s spell checker, don’t forget to exit the Word application.
You now have a way of exploiting Microsoft Word to incorporate spell checking into your other scripting projects. How about a little scripting challenge?
Can you figure out a way to create an HTML application that will act as a stand-alone spell checker? The rules are simple:
Your HTA should provide a text box for the user to input a word to spell check.
It should also provide a submit button.
It should notify the user if the word is spelled correctly and list the suggested spelling if it isn’t.
And there you have it, a few new scripting tricks to use with Word’s OLE automation object. Harnessing the power of the Microsoft Word application can greatly improve your existing scripts or even provide functionality that is not otherwise available. I hope that I’ve provided some insight into the possibilities of scripting Microsoft Word. Now it’s your turn to play. Until next time, keep coding!