使Word的自动拼写检查拾取字典更改

发布于 2024-09-29 03:31:41 字数 661 浏览 0 评论 0原文

在 MS Word 中,可以将单词添加到自定义词典中,以便识别它们。如果某个单词未被识别,Word 会自动在其下方放置一条红色波浪线。如果您将该单词添加到自定义词典中,该行就会消失。我想做的是通过宏自动执行这个过程。看来必须手动打开词典文件并写入新单词,因为单词词典对象上没有方法将单词添加到给定的词典中。这没有问题,只是 Word 不会自动选取新单词并删除新添加单词下方的红色波浪线。我什至尝试清除自定义词典并将其添加回来,但在您手动运行拼写检查之前,它似乎不会重新加载词典。示例代码如下:

Dim x As Dictionary
Dim fname As String

fname = "C:\Users\me\AppData\Roaming\Microsoft\UProof\md.dic"

' code to add word to dictionary goes here

With CustomDictionaries
    .ClearAll
    .Add fname
    .ActiveCustomDictionary = CustomDictionaries.Item(fname)

End With

有没有办法让 Word 识别自定义词典中新添加的单词而不运行交互式拼写检查?如果您手动添加单词,它会默默地执行此操作,但我似乎无法在 VBA 中复制此行为。我希望红线自动消失,就像您手动添加单词时一样。

In MS Word it's possible to add words to a custom dictionary so they're recognized. If a word is not recognized, Word automatically puts a red squiggly line underneath it. If you add that word to the custom dictionary, this line disappears. What I'd like to do is perform this process automatically via a macro. It appears that one has to manually open the dictionary file and write the new word, as there is no method on the Word Dictionary object to add words to a given dictionary. This is no problem, except that Word doesn't automatically pick up the new word and remove the red squiggly lines underneath the newly-added word. I've even tried clearing the custom dictionaries and adding them back in, but it doesn't seem to reload the dictionary until you manually run a spell check. Sample code for this follows:

Dim x As Dictionary
Dim fname As String

fname = "C:\Users\me\AppData\Roaming\Microsoft\UProof\md.dic"

' code to add word to dictionary goes here

With CustomDictionaries
    .ClearAll
    .Add fname
    .ActiveCustomDictionary = CustomDictionaries.Item(fname)

End With

Is there any way to make Word recognize the newly added word(s) in a custom dictionary without running the interactive spell check? It does this silently if you manually add words, but I can't seem to replicate this behavior in VBA. I'd like the red lines to go away automatically just like they do when you manually add words.

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

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

发布评论

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

评论(1

梦幻的心爱 2024-10-06 03:31:41

我还没有完全解决这个问题,但我想我已经找到了解决方法。您可以使用 ActiveDocument.SpellingErrors 获取表示拼写错误的 Range 对象的集合。我将在此集合中搜索与我添加到词典中的单词相匹配的文本,然后在该对象上设置 .NoProofing = True。这似乎会使红线消失,并且将新单词添加到词典中将阻止它们在我下次打开文档时再次出现。我还没有完全测试这种方法,但它看起来很有希望。

编辑
这种方法是有缺陷的,因为在同一会话期间输入的单词的其他实例将在其下方显示红色波浪线,因为它们尚未被明确忽略,并且拼写检查尚未使用更新的词典。如果您只是手动拉出自定义词典对话框并单击“确定”,后台会发生一些事情来重新读取词典。我只是不知道如何在代码中做到这一点。

I haven't exactly solved the problem, but I think I figured out a work around. You can get a collection of Range objects which represent spelling errors using ActiveDocument.SpellingErrors. I'm going to search this collection for text matching the word I've added to the dictionary, and then set .NoProofing = True on the object. This appears to make the red lines go away, and having added the new word to the dictionary will prevent them from coming back the next time I open the document. I haven't fully tested this approach, but it looks promising.

EDIT
This approach is flawed, as additional instances of the word which are entered during the same session will have the red squigglies underneath them because they haven't been explicitly ignored and the spell check isn't yet using the updated dictionary. If you just pull up the custom dictionary dialog manually and click OK, something happens in the background to re-read the dictionary. I just can't figure out how to do this in code.

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