如何 运行 在没有用户输入的情况下对字符串进行拼写检查?

How to run a spell check on a string without user input?

我正在尝试 运行 使用单词对字符串进行拼写检查,但我不想手动检查每个单词并确定正确的拼写。有没有办法自动执行此操作,以便它自己更正所有拼写错误的单词?到目前为止,这是我的代码,它可以工作,但我必须检查每个单词并点击更改...

If Address.Length > 0 Then
    Dim word_server As New Word.Application
    word_server.Visible = False
    Dim doc As Word.Document = word_server.Documents.Add()
    Dim rng As Word.Range

    rng = doc.Range()
    rng.Text = Address
    doc.Activate()
    doc.CheckSpelling()
    Address = doc.Range().Text
    doc.Close(SaveChanges:=False)
    word_server.Quit()
    doc = Nothing
    rCell.Value = Address
End If

这些网站可能会有所帮助。网站中的示例代码显示了如何调用 Word 进行拼写检查。您应该能够修改它,以便与您的代码一起使用。

http://www.vb-helper.com/howto_net_spellcheck.html

http://www.vbforums.com/showthread.php?307151-SPELL-CHECK-and-WORD

使用GetSpellingSuggestions函数绕过GUI。查看是否有任何拼写建议,然后您可以将其设置为第一个建议(这可能是个坏主意)。

您想如何确定正确的拼写是什么? "spon" 应该是 spoon、span、spin、spun 还是 son?此代码乐观地假设第一个建议(如果存在)是正确的解决方案。

声明:

Dim oError As Word.Range

然后替换:

  doc.Activate()
  doc.CheckSpelling()

与:

For Each oError In rng.SpellingErrors
    If oError.GetSpellingSuggestions.Count > 0 Then
        oError.Text = oError.GetSpellingSuggestions().Item(1).Name
    Else
        'Uh oh, no suggestions, do something here.
    End If
Next