使用 Interop 的 Word 2007 拼写检查时,某些语言不起作用
我在 VB.net 桌面应用程序中通过 Interop 使用 Word 2007 拼写检查器。当使用默认语言(英语)时,它工作正常。如果我通过 LanguageId 将语言设置为法语,它也可以工作。但如果我将其设置为法语(加拿大)(Word.WdLanguageID.wdFrenchCanadian),它就不起作用。没有错误消息,它只是运行并表示文档不包含错误。
我知道确实如此,如果我将完全相同的文本粘贴到 Word 本身并使用法语(加拿大)词典运行它,它会发现错误。为什么那本词典不起作用对我来说有点神秘。
完整代码如下:
Public Shared Function SpellCheck(ByVal text As String, ByVal checkGrammar As Boolean) As String
' If there is no data to spell check, then exit sub here.
If text.Length = 0 Then
Return text
End If
Dim objWord As Word.Application
Dim objTempDoc As Word.Document
' Declare an IDataObject to hold the data returned from the
' clipboard.
Dim iData As IDataObject
objWord = New Word.Application()
objTempDoc = objWord.Documents.Add
objWord.Visible = False
' Position Word off the screen...this keeps Word invisible
' throughout.
objWord.WindowState = 0
objWord.Top = -3000
' Copy the contents of the textbox to the clipboard
Clipboard.SetDataObject(text)
' With the temporary document, perform either a spell check or a
' complete
' grammar check, based on user selection.
With objTempDoc
.Content.Paste()
.Activate()
.Content.LanguageID = Word.WdLanguageID.wdFrenchCanadian
If checkGrammar Then
.CheckGrammar()
Else
.CheckSpelling()
End If
' After user has made changes, use the clipboard to
' transfer the contents back to the text box
.Content.Copy()
iData = Clipboard.GetDataObject
If iData.GetDataPresent(DataFormats.Text) Then
text = CType(iData.GetData(DataFormats.Text), _
String)
End If
.Saved = True
.Close()
End With
objWord.Quit()
Return text
End Function
I'm using the Word 2007 spellchecker via Interop in a VB.net desktop app. When using the default language (English), it works fine. If I set the language to French via LanguageId, it also works. But if I set it to French (Canadian) (Word.WdLanguageID.wdFrenchCanadian), it doesn't work. There's no error message, it simply runs and says the document contains no errors.
I know it does, if I paste the exact same text into Word itself and run it with the French (Canadian) dictionary, it finds errors. Just why that dictionary doesn't work is kind of a mystery to me.
Full code below:
Public Shared Function SpellCheck(ByVal text As String, ByVal checkGrammar As Boolean) As String
' If there is no data to spell check, then exit sub here.
If text.Length = 0 Then
Return text
End If
Dim objWord As Word.Application
Dim objTempDoc As Word.Document
' Declare an IDataObject to hold the data returned from the
' clipboard.
Dim iData As IDataObject
objWord = New Word.Application()
objTempDoc = objWord.Documents.Add
objWord.Visible = False
' Position Word off the screen...this keeps Word invisible
' throughout.
objWord.WindowState = 0
objWord.Top = -3000
' Copy the contents of the textbox to the clipboard
Clipboard.SetDataObject(text)
' With the temporary document, perform either a spell check or a
' complete
' grammar check, based on user selection.
With objTempDoc
.Content.Paste()
.Activate()
.Content.LanguageID = Word.WdLanguageID.wdFrenchCanadian
If checkGrammar Then
.CheckGrammar()
Else
.CheckSpelling()
End If
' After user has made changes, use the clipboard to
' transfer the contents back to the text box
.Content.Copy()
iData = Clipboard.GetDataObject
If iData.GetDataPresent(DataFormats.Text) Then
text = CType(iData.GetData(DataFormats.Text), _
String)
End If
.Saved = True
.Close()
End With
objWord.Quit()
Return text
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
拼写检查器是否实际安装了不起作用的语言?
如果您尝试将语言更改为德语? (或意大利语但不是意大利语(瑞士))
Are the languages which don't work actually installed for the spellchecker?
And if you try to change the language to for example German? (or Italian but not Italian (Switzerland))