Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

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

ltlbearand3
CERTIFIED EXPERT
Published:
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
                      Const DICTIONARYNAME = "CUSTOM.DIC"
                      
                      ' 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
                      Else
                      	Wscript.Echo DICTIONARYNAME & " was not found."
                      	wcript.quit
                      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.open
                      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.Open 
                      objTempFile.WriteText strText
                      objTempFile.SaveToFile strTempFile, adSaveCreateOverWrite
                      
                      ' Close the Files
                      objOriginalFile.Close
                      objTempFile.Close
                      
                      ' --------------------------------------------------------------------------------
                      ' 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.Open
                      objNewFile.LoadFromFile strTempFile
                      objFSO.OpenTextFile(strFile, ForWriting, True, TristateTrue).Write objNewFile.ReadText
                      objNewFile.Close
                      
                      ' --------------------------------------------------------------------------------
                      ' 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

2
3,133 Views
ltlbearand3
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.