Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to add AutoCorrect entries to Word.
Microsoft Scripting Guy, Ed Wilson, is here. Cool. Tonight is the Microsoft TechEd 2012 closing party in Orlando, Florida. This year the party is at Island of Adventure, and the Scripting Wife has been “chomping at the bit” all week—she is dying for the party. The party at TechEd 2011 in Atlanta, Georgia was one of the most fun events I have ever attended. As a trained underwater photographer, I love watching fish anyway, and the Aquarium in Atlanta is one of the best in the world. Add to that a Coca Cola museum (never heard of such a thing), and you have a formula for success. The best thing about the party was not the venue (which was great) but chance to meet and to talk to so many scripters. We have every expectation that the party tonight will be at least as great.
Adding entries to the AutoCorrect feature in Microsoft Word
One of my favorite features in Microsoft Word is the AutoCorrect feature. Although the AutoCorrect feature is seemingly intended to correct common spelling errors such as recieve (instead of receive), I routinely add new letter combinations to it, such as WPS for Windows PowerShell. This saves me a good deal of typing. Adding new entries is not too much of a hassle. I just have to go to Options from the File menu, choose Proofing, and press the AutoCorrect Options button (this is shown in the menu that follows).
The problem comes when I use multiple computers. I know about using Microsoft tools to migrate my Office profile, or to export my user customization settings, but this seems like using an eight pound sledge hammer to drive finishing brads into crown molding—it will work, but it is a bit excessive. In the past I ended up adding the custom AutoCorrect entries manually as I came across them in my typing. Now I use a Windows PowerShell script to add them for me.
The AddAutoCorrectEntries.ps1 script illustrates the technique of using Windows PowerShell to add a custom AutoCorrect entry to Microsoft Word. As with all scripts that use Microsoft Word automation, the script begins with creating an instance of the Word.Application COM object. To do this, use the New-Object cmdlet and the ComObject parameter. I store the returning Word.Application COM object in a variable named $word. This command is shown here.
$word = New-Object -ComObject word.application
Next, I set the Visible property of the Word.Application object to $false. This will prevent the Word application from appearing when Windows PowerShell creates the object. There is no reason to make Word visible, and from an automation standpoint, it is actually desirable to keep it from becoming visible. This line of code is shown here.
$word.visible = $false
Next I need to retrieve the entries collection from the AutoCorrect object. I store the returning collection in the $entries variable as shown here.
$entries = $word.AutoCorrect.entries
The entries collection contains a method named Add. When adding an entry to the AutoCorrect entries collection, I first supply the item to correct, and then the thing with which to correct it. Therefore, the command that is shown here detects gps, and it automatically corrects it to Get-Process. I pipe the entry object that returns from the command to Out-Null because I have no further need to do anything with it.
$entries.add(“gps”,”Get-Process”) | out-null
That is basically all there is to using Windows PowerShell to add an entry to the AutoCorrect entries collection. Because I do not open the Microsoft Word application, I need to specifically exit the Word process. I do this by using the Quit method from the Word.Application object that is stored in the $word variable. I then assign $null to the $word variable and call garbage collection. This portion of the script is shown here.
$word.Quit()
$word = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
One thing to keep in mind is that when you close Microsoft Word and open it later, the AutoCorrect entries remain. In other words, these are permanent entries—that is, permanent until you delete them. The complete AddAutoCorrectEntries.ps1 script is shown here.
AddAutoCorrectEntries.ps1
$word = New-Object -ComObject word.application
$word.visible = $false
$entries = $word.AutoCorrect.entries
$entries.add(“gps”,”Get-Process”) | out-null
$word.Quit()
$word = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
Microsoft Word Automation Week will continue tomorrow when I will talk about reading a text file to add several AutoCorrect entries at one time. In addition, I will show how to read that same text file to remove the AutoCorrect entries. By using this technique, it becomes possible to customize Microsoft Word AutoCorrect entries for a single session—but hey, that is tomorrow’s blog. See ya then.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
0 comments