Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell and a CSV file to add bulk AutoCorrect entries to Microsoft Word.
Microsoft Scripting Guy, Ed Wilson, is here. Microsoft TechEd 2012 in Orlando, Florida is over—well, basically over. Luckily, the Scripting Wife and I were invited by one of the Windows PowerShell program managers to attend a post-event training session about Windows PowerShell 3.0. “Cool,” the Scripting Wife said, “For that, we will skip going to see a six-foot mouse (after all, it is still a rodent).” After the Windows PowerShell 3.0 session, we hop in the car and head to Jacksonville, Florida for the IT Pro Camp day-long event on Saturday. This is one of those “you don’t want to miss it” type of events. There are still a few tickets available, so if you are anywhere near the northeastern portion of Florida, you should check it out.
Note Todays blog is basically Part 2 about adding AutoCorrect entries to Microsoft Word. For Part 1, see yesterday’s Hey, Scripting Guy! blog, Use PowerShell to Add AutoCorrect Entries to Word.
Adding bulk AutoCorrect entries to Word
There is not much difference between adding bulk AutoCorrect entries or adding a single entry to the AutoCorrect feature. The same “overhead” applies associated with creating the Word.Application object and releasing the Word.Application object. The big difference is that entering a single entry from a command line or hardcoded into the script is a workable solution. With more than two or three entries, such a technique no longer remains viable. Therefore, storage of the bulk entries is a paramount design consideration for a script of this type. Because I might want to remove my bulk entries, I decided to write a single function to add or to delete bulk AutoCorrect entries.
Use a CSV file for storage
Perhaps the easiest way to add bulk AutoCorrect entries to Microsoft Word is to use a comma separated value (CSV) file to store the entries. Of course, XML is also a possibility, as would be an Access database, or a Microsoft Excel spreadsheet. But like I said, I am looking for the easiest storage, and there is nothing wrong with a CSV file, and Windows PowerShell makes working with CSV files very easy. To read a CSV file use the Import-CSV cmdlet and specify a path to the CSV file. For my CSV file, I used the same column headings that Microsoft Word uses in the graphical interface. Replace is the word to replace, and with is the word that makes the substitution. Part of the real power of this methodology is that I can substitute a few letters such as lol with a phrase such as laugh-out-loud. This can greatly reduce your typing requirements. My sample CSV file is shown here.
The beginning portion of my Set-AutoCorrectEntries function defines parameters for the path to the CSV file, and switched parameters that determine whether to add or to remove the entries from Microsoft Word. This portion of the function is shown here.
Function Set-AutoCorrectEntries
{
Param(
[string]$path,
[switch]$add,
[switch]$remove)
$entry = Import-Csv -Path $path
Use the Add method to add the entries
Next, I create the Word.Application object, do not make it visible, and obtain the AutoCorrect.entries object. This portion of the script is shown here.
$word = New-Object -ComObject word.application
$word.visible = $false
$entries = $word.AutoCorrect.entries
Now I determine if the function is to add or to delete the entries. If add, I use the ForEach language statement to walk through the collection of entries in the $entry variable (the result of importing the CSV file). I now use Try / Catch to provide basic error handling. In the Try scriptblock, I attempt to add the item to the entries collection from the AutoCorrect object. I use the Catch scriptblock, to catch any errors that arise and display the name of the replacement item that fails to add. This portion of the script is shown here.
if($add)
{
Foreach($e in $entry)
{
Try
{ $entries.add($e.replace, $e.with) | out-null }
Catch [system.exception]
{ "unable to add $($e.replace)" }
} #end foreach
}#end if add
Use the Delete method to remove entries from AutoCorrect
It takes a while to delete all the entries from the AutoCorrect entries object in Word. Therefore, I decide to use the Write-Progress cmdlet to produce a progress bar that indicates the status of the delete operation. I only add this progress bar to the Remove portion of the function because the Add portion completes quickly. First, I need to initialize the $j variable (used as a counter) to ensure that the correct percentage completion displays. Next, I use the ForEach language statement to walk through the entries stored in the $entry variable. I next increment the $j variable, and call the Write-Progress cmdlet. When this portion of the script runs, the dialog box shown here displays (if the script runs from within the Windows PowerShell ISE).
The portion of the script that begins the Remove operation and displays the progress bar is shown here.
if($remove)
{ $j = 0
Foreach($e in $entry)
{ $j = $j+1
Write-Progress -Activity "deleting entries" -Status "deleting $($e.replace)" `
-percentcomplete ($j/$entry.count*100)
To delete the entries in the AutoCorrect entries requires using the Delete method from the entries collection. To call this method, it is necessary to match an entry from the $entry collection that is created by reading the CSV file with an entry in the entries collection. This requires walking through the CSV file contents and the entries in the entries collection. When a match occurs, I call the Delete method. This portion of the script is shown here.
foreach($i in $entries)
{
if($i.name -eq $e.replace)
{ $i.delete() } }
} #end foreach entry
} #end if remove
The last thing to do is to close word and clean up. This code appears here.
$word.Quit()
$word = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
} #End function Set-AutoCorrectEntries
The complete AddRemoveAutoCorrectEntries.ps1 script appears here.
AddRemoveAutoCorrectEntries.ps1
Function Set-AutoCorrectEntries
{
Param(
[string]$path,
[switch]$add,
[switch]$remove)
$entry = Import-Csv -Path $path
$word = New-Object -ComObject word.application
$word.visible = $false
$entries = $word.AutoCorrect.entries
if($add)
{
Foreach($e in $entry)
{
Try
{ $entries.add($e.replace, $e.with) | out-null }
Catch [system.exception]
{ "unable to add $($e.replace)" }
} #end foreach
}#end if add
if($remove)
{ $j = 0
Foreach($e in $entry)
{ $j = $j+1
Write-Progress -Activity "deleting entries" -Status "deleting $($e.replace)" `
-percentcomplete ($j/$entry.count*100)
foreach($i in $entries)
{
if($i.name -eq $e.replace)
{ $i.delete() } }
} #end foreach entry
} #end if remove
$word.Quit()
$word = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
} #End function Set-AutoCorrectEntries
One thing to keep in mind, entries added to the AutoCorrect entries collection remain after closing Microsoft Word. But if Microsoft Word remains open during the process of adding or removing entries, the AutoCorrect entries do not update until after closing and reopening Microsoft Word.
Well, that is about all there is to using a CSV file to add or to delete entries for the AutoCorrect feature in Microsoft Word. This also concludes Microsoft Word Automation Week. Join me tomorrow for more cool Windows PowerShell stuff as I create a function to copy stuff from one Windows PowerShell ISE tab to a new one. This function is so cool that I added it to my Windows PowerShell ISE profile.
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