Some languages don't work when using Word 2007 Spellcheck from Interop
- by Tridus
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