[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Using VBScript to Update or Add To Microsoft's custom.dic

Published on
12,105 Points
2 Endorsements
Last Modified:
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID 28455246) Here. The asker wanted some help getting a script to update the custom.dic file for dictionary values in Microsoft Office. I thought no problem, it is just a text file. So I took a look at the code and it looked fine, but then I tried to run it and got the same error. I thought, well, that is strange, it is just a simple text file. Oh, but it is not.

When I opened it in Notepad++ I found that the encoding was not the standard utf-8, but rather it showed as UCS-2 Little Endian. I figured it cannot be that hard to write a script to update this file, so I took a whack at it. Needless to say after much searching in the internet and much more time than I had planned to spend on the question, I had nothing. That never feels good.

I took a break for the day and came back the next morning. Several ideas I found that should work did not. I had headed down the path of opening the file, saving it as UTF-8, and then opening it again and saving it back out. I found some scripts that could do that process, but all failed when I tried to add text. Eventually I stumbled across the right combination and sequence.
I decided since all the scripts on the internet that I could find to update MS Office’s dictionary assumed the older style file that was UTF-8, I needed to publish what I had found so others would not waste their time as I did.
First, in my script I wanted to account for the location of the file as Microsoft moved it between versions. I added that into my code. Once I find the file, the concept that worked for me was to open the file using the ADO stream with the Unicode character set. I then update the text and save it off as UTF-8 with the ADO Stream.

That way the new text and the old text all have the same encoding at that point. Now here you would think I could just convert the stream to UTF-8, add the text, convert it back and save it with the ADO stream, but I could never get that to work. I ended up saving it as a temp file with the UTF-8 encoding.

Now I have to get this back to Unicode. Therefore I open the file up with the ADO stream again and this time write it back out with the File System Object using the Unicode flag. Now when I look at the dictionary my new words have been added and the file is in the correct encoding.
Now in the script, you have to tell it what words you want added to the custom dictionary.  You will need to update this code section in the full script:
' Add Your Text That is to be inserted into the Dictionary
strText = objOriginalFile.ReadText
strText = strText  & "Test1" & vbcrlf
strText = strText  & "Test2" & vbcrlf

Open in new window

Currently, using this code, it will add the words, "Test1" and "Test2" to the custom dictionary.  You can just update these lines to add your words or you could add some separate code that could read from a separate file and add the words from that file.  If you wanted, you could also use the replace function to remove a word with some code like this:

strText = Replace(strText, "WordToReplace" & vbcrlf, "")

Open in new window

It is a lot of code for something that should be simple, but at this point it works and that is what is important to me.  Enjoy the code.
' ExpertExchange 
' Expert: ltlbearand3 [http://www.experts-exchange.com/M_2469312.html]
' Add a value or values to Microsoft's Custom.Dic via script
' The file encoded in UCS-2 Little Endian (according to notepad++)
' Must open the file convert to UTF-8, add your words, and convert back

Option Explicit
' --------------------------------------------------------------------------------
'  Set Up Variables, Define Constants and Instantiate objects
' --------------------------------------------------------------------------------
Dim objFSO, objOriginalFile, objTempFile, objNetwork, objNewFile, objShell
Dim strPath1, strPath2, strFile, strTempFile, strText

' Set Constants
Const adTypeText   = 2   
Const adSaveCreateOverWrite = 2
Const ForWriting = 2
Const TristateTrue = -1 'Opens Files as Unicode

' Instantiate Objects
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Set objShell = CreateObject("WScript.Shell")
Set objOriginalFile = CreateObject("ADODB.Stream")  
Set objTempFile = CreateObject("ADODB.Stream")
Set objNewFile = CreateObject("ADODB.Stream")  

' --------------------------------------------------------------------------------
'  Set up the File Location (Microsoft Office moves this file around based on version)
' --------------------------------------------------------------------------------
' Set Values for possible file location based on MS Office Version
strPath1 = "C:\Users\" & objNetwork.UserName & "\AppData\Roaming\Microsoft\Spelling\en-US\"
strPath2 = "C:\Users\" & objNetwork.UserName & "\AppData\Roaming\Microsoft\UProof\"

' Find the location of the Dictionary file as it changes based on version
If objFSO.FileExists (strPath1 & DICTIONARYNAME) Then
	strFile = strPath1 & DICTIONARYNAME
ElseIf objFSO.FileExists (strPath2 & DICTIONARYNAME) Then
	strFile = strPath2 & DICTIONARYNAME
	Wscript.Echo DICTIONARYNAME & " was not found."
End If

' Set Temp File
strTempFile = objShell.ExpandEnvironmentStrings("%TEMP%") & "\" & "TempDictionary.Dic"

' --------------------------------------------------------------------------------
' Open in ADO Stream to convert to UTF-8 and save in Temp File
' --------------------------------------------------------------------------------
' Open original File
objOriginalFile.Type = adTypeText
objOriginalFile.Charset = "unicode"
objOriginalFile.LoadFromFile strFile

' Add Your Text That is to be inserted into the Dictionary
strText = objOriginalFile.ReadText
strText = strText  & "Test1" & vbcrlf 
strText = strText  & "Test2" & vbcrlf

' Save as UTF-8 File
objTempFile.Type = adTypeText   
objTempFile.Charset = "utf-8"
objTempFile.WriteText strText
objTempFile.SaveToFile strTempFile, adSaveCreateOverWrite

' Close the Files

' --------------------------------------------------------------------------------
' Now Open the UTF-8 file and use the File System Object to save it back as Unicode(UCS-2 Little Endian)
' --------------------------------------------------------------------------------
objNewFile.Type = adTypeText
objNewFile.Charset = "utf-8"
objNewFile.LoadFromFile strTempFile
objFSO.OpenTextFile(strFile, ForWriting, True, TristateTrue).Write objNewFile.ReadText

' --------------------------------------------------------------------------------
' Clean Up
' --------------------------------------------------------------------------------
Set objFSO = Nothing
Set objNetwork = Nothing
Set objNewFile = Nothing
Set objOriginalFile = Nothing
Set objTempFile = Nothing

Wscript.Echo strFile & " has been updated."

Open in new window

1 Comment

Administrative Comment

by:Eric AKA Netminder

Congratulations; your article has been published, and has received EE-Approved status as well.

Page Editor

Featured Post

Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Join & Write a Comment

Wrapper-1-Query. Use an Excel function to calculate a column for an Access query. Part 1. Shows a query in Access that has a calculated column with the results of an Excel worksheet function. See how to call a wrapper function from a query, and …
Watch this online video tutorial and learn the best way to reduce Outlook mailbox size using Compact Now feature of Outlook. It removes the deletes item's space from Microsoft Outlook 2016, 2013, and 2010 and compresses the PST file size. This will …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month