How to change the dictionary language for spellcheck

D

D King

I need to know how to programmatically change the dictionary language for
spellcheck. I have the following code in my .net app which works great but
only checks spelling in the current word dictionary, I need to be able to
change this....any help is appreciated. Thanks.

Namespace SpellChecker

Public Class Spelling

Public Shared Function Check(ByVal TextToCheck As String) As String

TextToCheck = Trim(TextToCheck)

Try

' Create Word and temporary document objects.

Dim objWord As Object

Dim objTempDoc As Object

' Declare an IDataObject to hold the data returned from the clipboard.

Dim iData As IDataObject

' If there is no data to spell check, then exit sub here.

If TextToCheck = "" Then

Exit Function

End If

objWord = New Word.Application

objTempDoc = objWord.Documents.Add

objWord.Visible = False

' Position Word off the screen...this keeps Word invisible

objWord.WindowState = 0

objWord.Top = -3000

' Copy the contents of the textbox to the clipboard

Clipboard.SetDataObject(TextToCheck)

' With the temporary document perform spell check

With objTempDoc

..Content.Paste()

..Activate()

..CheckSpelling()

' .CheckGrammar()

' 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

'Return the text..

Check = CType(iData.GetData(DataFormats.Text), _

String)

End If

..Saved = True

..Close()

End With

objWord.Visible = False

objWord.Quit()

MessageBox.Show("The spelling check is complete.", _

"Spell Checker", MessageBoxButtons.OK, _

MessageBoxIcon.Information)

' Microsoft Word must be installed.

Catch COMExcep As COMException

MessageBox.Show( _

"Microsoft Word must be installed for Spell/Grammar Check " _

& "to run.", "Spell Checker")

Catch Excep As Exception

MessageBox.Show("An error has occured.", "Spell Checker")

End Try

End Function

End Class

End Namespace
 
C

Cindy M -WordMVP-

Hi D,
I need to know how to programmatically change the dictionary language for
spellcheck. I have the following code in my .net app which works great but
only checks spelling in the current word dictionary, I need to be able to
change this....
As a general rule, Word prefers to use the dictionary for the language
formatting applied to the currently selected text in the document. Below my
signature, I've copied a message from someone who did manage to get this to
work; I've never tested it, however.

Cindy Meister

Automating spell check from outside Word
From: (e-mail address removed) (Richard Pearce)
Newsgroups: microsoft.public.word.international.features
Subject: MainDictionary parameter of CheckSpelling method does not work:
Workaround
Date: 1 Mar 2002 04:39:33 -0800

I've been looking at using Word 2000 Visual Basic to programmatically
spell check text in a foreign language (i.e. a language other than the
operating system one), without using a Document object and all the
dialog interaction that entails. Example: My Windows installation is
English, and I want to find out if "Dien" is a valid German word
without bringing up the Spelling and Grammar dialog.

My first attempt used Application.CheckSpelling, passing the NameLocal
of the desired language to the MainDictionary parameter. As several
old threads point out, this doesn't work - the MainDictionary
parameter is ignored and the spell check is performed in the operating
system language, like it or not.

I've found a different way of approaching the problem, using the
SpellingSuggestions collection. The following code achieves the
desired result (apologies if the code is inelegant or inefficient -
I'm not a VB programmer):


SpellWord = "Dien"
DictLang = wdGerman

MainDict = Languages(DictLang).NameLocal
Set Suggest = GetSpellingSuggestions(Word:=SpellWord, _
CustomDictionary:="Custom.dic", MainDictionary:=MainDict)

If Suggest.SpellingErrorType = wdSpellingCorrect Then
MsgBox "Spelling OK"
Else
If Suggest.Count = 0 Then MsgBox "No Suggestions"
End If

For i = 1 To Suggest.Count
MsgBox Suggest.Item(i).Name
Next i
 
D

DotNetManiac

Thanks Cindy, actually I re-wrote the code from scratch as a class library
which can be used with any .net application. For the langague issue, I used
Range.LanguageID = wdWhateverLangauge

The full source code and project are available on my web site if anyone
wants it:
http://www.DotNetManiac.com/Winforms/0009.aspx

D.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top