【发布时间】:2016-01-11 08:54:59
【问题描述】:
大家早上好, 这几天一直在玩这个,没有得到任何地方。我在 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
【问题讨论】:
-
可以选择 WPF 吗? WPF 在
RichTextBoxes 中内置了拼写检查。 -
我不这么认为..?该程序是基于 Windows 窗体的大型程序(10 多年前,数百万行代码)。查看了几个将 wpf 控件集成到 Windows 窗体基础的示例,但看起来比这更复杂,并且希望保持一切与基础相同。
-
我了解您尝试模拟的代码工作正常;为什么不从这个(=工作版本)开始并继续删除/添加功能?例如:为什么要写 ` Dim activedoc As Word.Document = wapp.Documents.Add(, , , oFalse)` 而不是原始版本? (
m_app.Documents.Add()或m_app.Documents.Add_ (m_template, m_newTemplate, m_documentType, m_visible))?Documents.Add可以包含空白文档或从给定路径加载 Word 文件;并且您的代码似乎没有在这方面完全定义(没有提供路径,尽管有一些参数)...... -
... 无论如何,问题显然出在 Word Interop 部分;因此,您应该将调试重点放在它上面。其他替代方法可能是创建一个通过互操作连接到 Word 的小型应用程序(您可以在快速研究后轻松找到许多小代码)并对其进行所有最终测试(即打开文档并执行所有拼写检查操作)。这似乎是一个界限分明的问题,在正确的测试条件下,应该很快得到解决。但最好在完全理解 Word 部分之前不要添加 RichTextBox 复杂性。
-
一开始我确实直接复制/粘贴。在 IsWrongWord() 函数中遇到了完全相同的错误,这让我开始着手改变,看看哪里出了问题。到目前为止,除了获取拼写建议的那一行代码之外,一切都可以正常工作。
标签: .net vb.net ms-word office-interop spelling