Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Published on
11,827 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

Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Join & Write a Comment

Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month