Word Autocorrect

If you have been following this blog for awhile you will know that I build and rebuild machines on a reasonably frequent basis.  One drawback to this that I have to keep re-creating the Autocorrect entries.  I do a lot of technical writing much of which involves full names of products and correct capitalisation.  I saw an entry on the TechNet scripting center that showed how to add an entry to the autocorrect list.  It was in VBScript but thats not an issue as we can easily convert it to something more useful  🙂

I ended up with a module consisting of four functions

  • Get-AutoCorrectEntry
  • Export-AutoCorrectEntry
  • Add-AutoCorrectEntry
  • Import-AutoCorrectEntry

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
#Requires -version 2.0
function Get-AutoCorrectEntry {
    param (
        [switch] $all
    )
    $word = New-Object -ComObject Word.Application
    if ($all) {$word.AutoCorrect.Entries}
    else {$word.AutoCorrect.Entries | Select Name, Value}
    $word.Quit()
}
function Export-AutoCorrectEntry {
    param (
        [parameter(Mandatory=$true)]
        [string] $path
    )
   
    Get-AutoCorrectEntry | where {$_.Value -ne "*"} | Export-Csv -Path $path -NoTypeInformation
}
function Add-AutoCorrectEntry {
    param (
        [parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
        [string] $name,
       
        [parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
        [string] $value
                      
    )
    begin {
        $word = New-Object -ComObject Word.Application
        $ace = $word.AutoCorrect.Entries
    }
    process {$ace.Add($name, $value)}
    end {$word.Quit()}
   
}
function Import-AutoCorrectEntry {
    param (
        [parameter(Mandatory=$true)]
        [string] $path
    )
    Import-Csv -Path $path | Add-AutoCorrectEntry

}

 

Get-AutoCorrectEntry creates an object for Word and dumps the contents of the AutoCorrect Entries collection.  Default is to just display the name and the value where name is the bad text and value is the replacement text.  The –all switch dumps everything but we won’t usually need that info

Export-AutoCorrectEntry calls Get-AutoCorrectEntry add dumps the results to a file using Export-Csv.  Using where {$_.Value -ne "*"} restricts the output to the entries I’ve created rather than including Word’s own entries

Add-AutoCorrectEntry creates a new entry.  It takes name and value parameters then creates an object for word, gets the AutoCorrect Entries then uses the Add method to create the new entry.  I’ve used the begin/process/end scriptblocks so it behaves nicely on the pipeline

Import-AutoCorrectEntry reads a csv file then calls Add-AutoCorrectEntry

Next time I re-install Office I can dump the entries and then import them after installation.  I can even move my entries between machines.  I created this using Windows 7 and Office 2010 TP but there is no reason for it not to work with other versions of Office.

 

Advertisements
This entry was posted in PowerShell V2. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s