Hey, Scripting Guy! How Can I Add a New Item to the Microsoft Office Word AutoCorrect List?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I add a new item to Office Word’s AutoCorrect list?

— OK

SpacerHey, Scripting Guy! AnswerScript Center

Hey, OK. To begin with, we should point out that we have no way of knowing whether your initials really are O and K; after all, they could just as easily be TR, and you could actually be Tony Romo, quarterback of the Dallas Cowboys, writing to us under an assumed name. (Which, if you value your reputation, is the only kind of name you should use when writing to the Scripting Guys.) If that’s the case, then we want you to know that we have nothing against you personally. (Granted, we’ll never be convinced that you deserved to make the Pro Bowl this year, but that’s not really your fault.) Having said that, however, it’s also true that we do have all sorts of things against the Dallas Cowboys. Because of that we’re actually quite happy that your fumbled snap helped ensure that the Cowboys lost to the Seattle Seahawks in the opening round of the NFL playoffs. Thanks, man!

Note. Yes, we know: every religion ever created tells us that we should love thy neighbor. On the other hand, most religions are pretty old and were established long before there was such a thing as the Dallas Cowboys. If someone started a new religion today things might be different.

Besides, it’s not like we’re the only ones who don’t like the Cowboys; heck, even many of the Cowboy players don’t seem to like the Cowboys. The Scripting Guys are just trying to go along with the crowd.

Anyway, if it turns out that you are Tony Romo, well, yes, we are reveling in your misfortune, and we do feel a little bad about that. (Not much, mind you, but a little.) But while Saturday’s defeat means that you won’t get a Super Bowl ring this year, here’s something equally valuable: a script that adds a new item to Word’s AutoCorrect list:

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set colEntries = objWord.AutoCorrect.Entries
colEntries.Add "Fabirkam", "Fabrikam"

And no, this script isn’t four lines long because we refused to write more than four lines of code for the Dallas Cowboys. On the contrary: we have just four lines of code simply because all we need is four lines of code. That’s all it takes to add an item to the AutoCorrect list.

Note. So does that mean that if we did need more than four lines of code we would have gone ahead and done that extra work? Uh, sure, sure we would have. Hey, you know how the Scripting Guys feel about doing extra work.

As you can see, the script starts out by creating an instance of the Word.Application object, then sets the Visible property to True; that gives us a running instance of Word that we can see on screen. Note that you don’t have to make Word visible in order to add an item to the AutoCorrect list; if you want, go ahead and leave out the line of code that sets the Visible property to True. If you elect to do that just be sure to make this the last line of the script:

objWord.Quit

And yes, that is important: if you omit the Quit method Word will not terminate when the script ends, but will, instead, continue to run on forever and ever.

Sort of like the Dallas Cowboys’ playoff losing streak, which has now reached 10 years and counting.

Not that we’re counting, mind you.

As soon as Word is up and running we use this line of code to create an object reference to the AutoCorrect.Entries collection:

Set colEntries = objWord.AutoCorrect.Entries

From there all we need to do is call the Add method to add a new item to the collection:

colEntries.Add "Fabirkam", "Fabrikam"

As you can see, the Add method takes two parameters: Fabirkam (the common misspelling that we want to correct) and Fabrikam (the corrected spelling). From now on anytime we type Fabirkam Word will autocorrect the spelling the Fabrikam.

What if we change our mind and decide that we don’t want Fabirkam in the AutoCorrect list after all? No problem; here’s a simple little script that binds to the AutoCorrect.Entries collection, looks for an entry with the NameFabirkam, then uses the Delete method to remove that item from the collection:

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set colEntries = objWord.AutoCorrect.Entries
For Each objEntry in colEntries
If objEntry.Name = "Fabirkam" Then
objEntry.Delete
End If
Next

And yes, we have no doubt the Dallas Cowboys wish it was as easy to undo the actions of a football play as it is to undo the actions of the script. But the Scripting Guys would never say something like that; after all, the Cowboys probably feel bad enough without us rubbing it in.

Note. Besides, now the Seahawks have to travel to Chicago to take on the Bears, which means that the Cowboys shouldn’t worry too much: we’re likely to get our comeuppance soon enough. (But, hey, you never know, right?) Go Seahawks!

0 comments

Discussion is closed.

Feedback usabilla icon