大家早上好,玩了几天了,没找到。我正在 vb.net 中创建一个自定义的 Richtextbox,它会在拼写错误的单词被右键单击时强调拼写错误并提供建议(我不敢相信这不是文本框/richtextboxes 中的默认设置......无论如何)。我的下划线运行良好,但我不断收到错误消息:此命令不可用,因为没有打开文档。
编辑
在桌面上运行,64 位,已安装 Office 2007。这是整个班级以及我如何测试通话。IsWordWrong 效果很好。SpellingSuggestions 在 wapp.GetSpellingSuggestions(pWord) 上失败,并出现错误“此命令不可用,因为没有打开文档”,根据 MSDN 和我看过的多个教程,不应该发生:
Public Class SpellCheckUtility
Private Shared wapp As Word.Application
Private Shared missing As Object = Reflection.Missing.Value
Public Shared Sub StartApp()
If IsNothing(wapp) Then
wapp = New Word.Application
wapp.Visible = False
wapp.WindowState = 0
End If
End Sub
Public Shared Function IsWrongWord(ByVal pWord As String) As Boolean
StartApp()
Dim oFalse As Object = False
Dim activedoc As Word.Document = wapp.Documents.Add(, , , oFalse)
Dim m_range As Word.Range
m_range = activedoc.Range
m_range.InsertAfter(pWord)
Dim SpellErrors As Word.ProofreadingErrors = m_range.SpellingErrors
Return SpellErrors.Count > 0
End Function
Public Shared Function SpellingSuggestions(ByVal pWord As String) As Generic.List(Of String)
Dim rtnlist As New Generic.List(Of String)
If pWord.Length > 0 Then
StartApp()
Dim SpellErrors As Word.SpellingSuggestions = wapp.GetSpellingSuggestions(pWord)
For m_word As Integer = 1 To SpellErrors.Count
rtnlist.Add(SpellErrors.Item(m_word).Name)
Next
End If
Return rtnlist
End Function
Public Shared Sub dispose()
If Not (wapp Is Nothing) Then
Dim m_saveChanges As Object = False
wapp.Quit(m_saveChanges)
wapp = Nothing
End If
End Sub
End Class
怎么称呼:
Private Sub btnclick1_Click(sender As Object, e As EventArgs) Handles btnclick1.Click
Dim wordlist As Generic.List(Of String) = SpellCheckUtility.SpellingSuggestions("thingz")
End Sub
我已经尝试了 wapp.GetSpellingSuggestions 和 m_range.GetSpellingSuggestions 两者的结果相同。我在其他地方使用 m_range.SpellingErrors 并且效果很好,并且获取范围的设置完全相同,所以不确定我做错了什么。
任何帮助是极大的赞赏!!
**将此代码改编为我真正想要的http://www.codeproject.com/Articles/18799/Spell-check-and-underline-the-wrong-word-using-Mic