我需要检查 Word 文档中的各种代码。
这些代码通常是一个字母后跟一个连字符和 5 位数字,例如 M-81406。
我们需要检查这些预定义的代码是否输入正确(有几千个代码的预定义列表)。我们不能使用普通的 Word 拼写检查,因为您不能拼错数字(您至少需要 2 个字母)。
我想我可以编写 VBA 代码来发现任何可能无效的内容。我可以使用正则表达式来检查它们。
我可以从 VBA 访问自定义字典的内容吗?
我需要一个可由客户轻松更新的解决方案。
奖金指向任何提出突出错误代码的聪明方法的人。
您可能知道,自定义词典只是一个包含单词的文本文件,因此请参考 Microsoft Scripting Runtime,并使用:
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
不理想,因为它破坏了下划线格式,并且不提供建议机制(尽管围绕列表框构建的小表单就足够了)。
不幸的是,最好的解决方案是扩展拼写引擎。