Word VBA 访问自定义词典的内容

发布于 2024-07-08 15:32:15 字数 322 浏览 7 评论 0原文

我需要检查Word文档中的各种代码。

这些代码通常是一个字母后跟一个连字符和 5 个数字,例如 M-81406。

我们需要检查这些预定义代码是否已正确输入(有数千个代码的预定列表)。 我们无法使用普通的 Word 拼写检查,因为您不能拼错数字(您至少需要 2 个字母)。

我想我可以编写 VBA 代码来查找任何可能无效的代码。 我可以使用正则表达式来检查它们。

我可以从 VBA 访问自定义词典的内容吗?

我需要一个可由客户轻松更新的解决方案。

任何提出巧妙地突出显示错误代码的方法的人都会获得奖励。

I need to check various codes in a Word document.

These codes are usually a letter followed by a hyphen and 5 digits, eg M-81406.

We need to check that these pre-defined codes have been typed in correctly (there is a pre-determined list of several thousand codes).
We cannot use normal Word spell checking as you cannot misspell a number (you need at least 2 letters).

I figured I could write VBA code that finds any that may be invalid. I can use regular expressions to check them.

Am I able to access the contents of a custom dictionary from VBA?

I need a solution that is to be updated by the client easily.

Bonus points to anyone who suggests a clever way of highlighting the incorrect codes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2024-07-15 15:32:16

您可能知道,自定义词典只是一个包含单词的文本文件,因此请参考 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

不理想,因为它会破坏下划线格式,并且不提供建议机制(尽管有一个小的建议)围绕列表框构建的表单就足够了)。

不幸的是,最好的解决方案是扩展拼写引擎。

A custom dictionary, as you probably know, is just a text file containing the 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 it clobbers underlining formatting, and doesn't provide a mechanism for suggestions (although a small form built around a listbox would suffice).

The best solution would involve extending the spelling engine, unfortunately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文