Monthly Archive

Categories

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.

 

One Response to Word Autocorrect

  • Jens Grabig says:

    Hi Richard,
    I saw your Word Autocorrect script and I would like use the functionality although I am quite not familiar with Powershell scripting.
    Can you please tell how to use you script?

    I managed to install the Windows Powershell 2.0 on XP Pro SP3 (and I am using Word 2007.).

    I managed to set the scripting Excecution Policy to “unrestricted”.

    When your script is started, it tells me:

    Windows PowerShell
    Copyright (C) 2009 Microsoft Corporation. All rights reserved.

    PS C:\Dokumente und Einstellungen\JGrabig> cd\
    PS C:\> cd test
    PS C:\test> testscript.ps1
    The term ‘testscript.ps1’ is not recognized as the name of a cmdl
    the spelling of the name, or if a path was included, verify that
    At line:1 char:15
    + testscript.ps1 < <<< + CategoryInfo : ObjectNotFound: (testscript.ps1:Str + FullyQualifiedErrorId : CommandNotFoundException Suggestion [3,General]: The command testscript.ps1 was not found, hell doesn't load commands from the current location by default. s1". See "get-help about_Command_Precedence" for more details. **** Starting as .\testscript seems to work as I only receive a new prompt without any (error-)message. Where may I find the csv-file with the exported autocorrect entries? Do I have to adapt the script? Sorry for asking so low level questions. Is it nevertheless possible to use your script on this basis? Thanks a lot in advance, Jens Grabig info@grabig.de

Leave a Reply