My client recently changed there name for branding reasons, making it more concise. They have around 100 employees using Word with an Autotext (auto complete) entry for their old company name. For anyone who doesn't already know what the Autotext feature is in Word it is a special feature that will complete a phrase for you after you type the first couple of letters for it. This actual text for the Autotext is stored in the Normal.Dot file located deep within your file systems which is the default Word template. This template is loaded every time you open a new document. Outlook also uses Word as its editor and thus uses the Normal.dot.
Anyway, they wanted to change the Autotext entry for their company name on all 100 computers. This would have been quite a hassle for each user to do themselves or taken a lot of time for one person to run around to each computer and change it. So they had me write a script that would change the entry and could be set to run the next time the user logged in. At first I tried to edit the Normal.dot file manually through a filestream but I ran into problems when writing the changes back. Special characters and other items in the file header were either erased or changed and the file would be corrupted. So I reached out for some help and an IT guy sent me a link to some code that looked a lot like what I have below (I can't find the link or I would post it, only half of what's below I will claim as my own work, the easy half). I modified it to work for my situation and added some error checking in case it was run more than once and the old record wasn't found. It worked pretty well and for once I was assured I had saved the client some money. Oh how powerful vbscript can be.
To run the code save the text below to a text file such as RenameAutotext.vbs. Then from the command prompt run: cscript RenameAutotext.vbs. Run using cscript not wscript because if run using wscript will send the Echo command to the program running, Word, instead of writing it to the command line. This script is also test to work only for Windows XP and Microsoft Word 2003. Word 2007 uses a different type of template file so and this script will need to be modified to work with that.
There may be some extraneous parts that aren't 100% necessary below but with vbscript I am happy when I get it to do exactly what I want and leave it at that. If anyone has some suggestions for improvement leave a comment.
The Code:
'Rename an
'Get Application Data Path
Set oShell = CreateObject("WScript.Shell")
appdata = oShell.ExpandEnvironmentStrings("%APPDATA%")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open(appdata & "\Microsoft\Templates\Normal.dot", False)
'Add new autotext entries
Set objTxtEntry = objWord.NormalTemplate.AutoTextEntries.Add("New Company Name", objWord.Selection.Range)
objWord.NormalTemplate.AutoTextEntries("New Company Name").Value = "New Company Name"
'Enable error handling
On Error Resume Next
'Delete old autotext entries
objWord.NormalTemplate.AutoTextEntries("Old Company Name").Delete
If Err.Number <> 0 Then
WScript.Echo "No Old Company Name record found, new entry inserted."
Else
WScript.Echo "Completed Successfully."
End If
'Print all autotext entires to make sure it was changed properly
'Wscript.Echo
Dim i
For Each i In objWord.NormalTemplate.AutoTextEntries
Wscript.Echo i.Name
Next
objWord.Quit