【发布时间】:2011-01-31 10:08:03
【问题描述】:
我在 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
【问题讨论】:
标签: .net vb.net interop office-2007 spell-checking