早上好每个人, 已经玩了几天,没有在哪里。我正在vb.net中创建一个自定义的richtextbox,它会强调拼写错误,并在右键单击拼写错误的单词时提供建议(我不相信这不是textboxes / 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
答案 0 :(得分:0)
Rant over,这是我添加到StartApp()函数顶部的行,现在一切正常。
If Not IO.Directory.Exists("C:\Windows\SysWOW64\config\systemprofile\") Then IO.Directory.CreateDirectory("C:\Windows\SysWOW64\config\systemprofile\")
答案 1 :(得分:0)
我能够通过使用
解决与这些症状类似的问题Dim activedoc = wapp.Documents.Add()
而不是
Dim activedoc = wapp.Documents.Add(, , , False)