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