Word VBA, accessing custom dictionary content
I need to check various codes in a Word document.
These codes are usually a letter followed by a hyphen and 5 numbers, such as M-81406.
We need to check that these predefined codes are entered correctly (there is a predefined list of several thousand codes). We cannot use the regular Word spell checker as you cannot miss a number (you need at least two letters).
I figured I could write VBA code that detects any that might be invalid. I can use regular expressions to test them.
Is it possible to access the content of a custom dictionary from VBA?
I need a solution that is easy to update by the client.
The bonus points to anyone offering a clever way of counting incorrect codes.
source to share
A custom dictionary, as you probably know, is a text file containing words, so reference the Microsoft Scripting Runtime and use:
Dim FSO As New FileSystemObject, FS As TextStream
Dim Code As String, Codes As New Scripting.Dictionary
Dim Paragraph As Paragraph, Word As Range
Set FS = FSO.OpenTextFile("c:\...\cust.dic")
Do Until FS.AtEndOfStream
Code = FS.ReadLine
If Not Codes.Exists(Code) Then Codes.Add Key:=Code, Item:=Nothing
Loop
' Use your own method for enumerating words
For Each Paragraph In ActiveDocument.Paragraphs
Set Word = Paragraph.Range
Word.MoveEnd WdUnits.wdCharacter, -1
If Not Codes.Exists(Word.Text) Then
Word.Font.Underline = wdUnderlineWavy
Word.Font.UnderlineColor = wdColorBlue
Else
Word.Font.Underline = wdUnderlineNone
Word.Font.UnderlineColor = wdColorAutomatic
End If
Next
Not ideal because these clobbers emphasize formatting and do not provide a mechanism for suggestions (although a small form around the list will suffice).
The best solution, unfortunately, is to extend the writing mechanism.
source to share