Share via


Office Space: Tips and Tricks for Scripting Microsoft Office Applications

Office Space

Welcome to Office Space, the column that offers tips and tricks for scripting Microsoft® Office applications. We’ll post new tips every Tuesday and Thursday; to see an archive of previous tips, visit the Office Space Archive. And if you have particular questions about Microsoft Office scripting, feel free to send them to scripter@microsoft.com (in English, if possible). We can’t promise to answer all the questions we receive, but we’ll do our best.

Using the Spelling Checker in Microsoft Word

No doubt a lot of you are sitting around thinking, “I wonder how come the Scripting Guys haven’t been rampaging through the countryside, spray-painting ‘Scripting Rules!’ or ‘WMI Rocks!’ on every building, subway car, or other blank surface they can find. What’s up with that?”

That’s a fair question, and there are actually several reasons why we haven’t done this. For one, the Scripting Guys tend to get into enough trouble even when we aren’t asking for it; we rarely need to go out and hunt up reasons to get people mad at us. For another, spray-painting ‘Scripting Rules’ on blank walls all over the country sounds like a lot of work. Let’s face it: when was the last time you saw the words Scripting Guys and work in the same sentence?

Well, OK: except when the two are joined by the phrase don’t ever seem to.

Both very good points, but there’s an even more compelling reason why we don’t spray-paint messages everywhere we go: cans of spray-paint don’t come with a built-in spelling checker.

Yes, like many people in this day and age the Scripting Guys' spelling skills have become a bit rusty, to say the least. Is it occurrence or occurrance, or maybe even occurence? Ocassion or occasion? Hey, what difference does it make: the spelling checker will figure it out for us.

And that’s true, the spelling checker will figure it out for us … assuming, of course, that there is a spelling checker. Unfortunately, not every application has a spelling checker. One of the Scripting Guys, for example, types all his scripts using Notepad; Notepad doesn’t have a spelling checker. Ever wonder why all his message boxes seem so terse, and why they never include words with more than 3 or 4 letters? Now you know.

Of course, one way to solve this problem would be for the Scripting Guys to learn how to spell. But that seems terribly old-fashioned. Therefore, we came up with a different way to work around this issue: we’ll just tap into the spell-checking capabilities of Microsoft Word and create our own custom spelling checker application.

As this implies, the spelling checker – like pretty much everything else in Word – is accessible through scripting. Let’s start off by taking a look at a rudimentary script that reports back whether a given word is misspelled or not. (Or is that mispelled or not?) We’ll explain how the script works, then we’ll revamp it a little bit to make it more useful

Here’s our starter script:

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()

If objWord.CheckSpelling("mispelled") Then
    Wscript.Echo "The word is spelled correctly."
Else
    Set colSuggestions = objWord.GetSpellingSuggestions("mispelled")
    For Each strSuggestion in colSuggestions
        Wscript.Echo strSuggestion
    Next 
End If

objWord.Quit

We begin by creating an instance of the Word.Application object, then use the Add() method to create a new, blank document. You might note that we don’t set the Visible property to True in this script. That’s because for this script we want Word to open up in an invisible window, we don’t want to see it on screen.

Next we use this line of code to determine whether or not the word mispelled is spelled correctly:

If objWord.CheckSpelling("mispelled") Then

What we’re doing here is calling the CheckSpelling method, passing as a single parameter: the word to be checked. CheckSpelling will return a Boolean value: True if the world is spelled correctly, False if the word is not spelled correctly. If CheckSpelling returns True we echo a message that the word is spelled correctly, and then call the Quit method to terminate our invisible instance of Microsoft Word. At that point, our script gracefully comes to an end

But what if CheckSpelling returns False? As you well know, most times you misspell something Microsoft Word will offer you some suggested spellings. For example, if you type rin in Word, you can right-click the misspelled word and see a list of suggested spellings:

Spell Checking

And you’re absolutely right: we can return suggested spellings in our script. In fact, that’s exactly what we do if our target word is misspelled:

Set colSuggestions = objWord.GetSpellingSuggestions("mispelled")
For Each strSuggestion in colSuggestions
    Wscript.Echo strSuggestion
Next

Here we call the GetSpellingSuggestions method, passing along the word that we misspelled. GetSpellingSuggestions returns an array of suggested spellings for the word. (Note that sometimes even Word can’t figure out what you were trying to spell. In that case, there might not be any suggested spellings, and the array might come back empty.) After calling the method we simply walk through the returned items, echoing each suggested spelling to the screen. When we run this particular script we should get output that looks like this:

misspelled

There you have it: the suggested spelling for the word mispelled. Cool.

Well, sort of cool. This works really well, but every time you want to check the spelling of a word you’ll need to modify the script—after all, we hard-coded in the value mispelled. (Not only that, you have to modify the script in two places. We won’t mention which Scripting Guy ran the revised script three times before figuring out why checking the spelling on rin kept returning misspelled.) On top of that, each time you run the script there will be a momentary delay while you wait for Word to load. You know what really would be cool? Imagine a script that runs in a command window. When the script starts, it loads up Word, then asks you to type a word to be checked. You do so, the script checks the word, and then it loops around and asks for the next word to be checked. This continues until you just hit ENTER, without typing in a word, at which time the script terminates both itself and your instance of Word.

You’re right: it is kind of hard to imagine a script like that, isn’t it? So let’s just show you the code instead:

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()

Do While True

WScript.StdOut.Write "Please enter the word to be checked: " 


strInput = WScript.StdIn.ReadLine

If strInput = "" Then
    Exit Do
End If

If objWord.CheckSpelling(strInput) Then
    Wscript.Echo "The word is spelled correctly."
Else
    Set colSuggestions = objWord.GetSpellingSuggestions(strInput)
    For Each strSuggestion in colSuggestions
        Wscript.Echo strSuggestion
    Next 
End If

Wscript.Echo

Loop

objWord.Quit

Before trying this script note that it must run in a command window and that it must run under CScript; that’s because it uses StdIn and StdOut, which are not available under WScript. We won’t discuss StdIn and StdOut in today’s column; if you aren’t sure what those things do then take a peek at the Microsoft Windows 2000 Scripting Guide.

Instead, we’ll just show you a picture of our little custom spelling checker in action:

Spell Checking

As you can see, we typed rin and the script responded by showing us a list of suggested spellings. You might notice as well that we actually get more suggestions via the script than we do via right-clicking a misspelled word; that’s because Word limits the number of suggestions that appear when you right-click a word. With a script we get the same set of suggestions we would get if we actually ran the spelling checker.

Granted, for 2 bucks we probably could have gone out and bought a little paperback dictionary and looked up spellings there. But where’s the fun in that? Besides, being able to tap into Word’s spelling checker opens up a world of possibilities for you. For example, you could add a spelling checker to your HTA. That lies a bit outside the scope of this column, but it’s very possible. Hey, when it comes to scripting and Microsoft Word, the sky’s the limit.

So, armed with a custom spelling checker that we can cart around on a laptop are we now ready to go around spray painting ‘ADSI Forever!’ on blank walls and subway cars? Well, we wouldn’t count on it. We’ve solved the spelling problem, but there’s still that whole laziness issue to deal with. For now, we’ll limit our writing to scripting columns. Goodbey and see you next week.

Uh, make that goodbye and see you next week.