off5

 

Use MS Word Spell Checker To Check Spelling In Your Program


Use MS Word Spell Checker To Check Spelling In Your Program

This example will launch the Word spell checker to check the spelling in
text box, and correct them.


Preparations

Add 1 Command Button and 1 Text Box to your form.
Set the text box Multiline property to True (Optional).


Form Code


Private Sub Command1_Click()

Dim objWord
Dim tmpObjWord
Dim strResults

' Only continue if user has typed text into the text box.
If Len(Text1.Text) < 1 Then Exit Sub

Set tmpObjWord = CreateObject("Word.Application")
' check if there are any spelling errors.
If tmpObjWord.CheckSpelling(Text1.Text) Then
MsgBox "The text spelled correctly"
' free memory
Set tmpObjWord = Nothing
' exit sub - No spelling errors are found.
Exit Sub
End If

'free memory
Set tmpObjWord = Nothing
Set objWord = CreateObject("Word.Application")
With objWord
' hide the Word application
.Visible = False
' Spell checker only works within a document
.Documents.Add

' Put the text in the document
.Selection.TypeText Text1.Text

' disallow grammer checking. To allow it set it to "True"
.Options.CheckGrammarWithSpelling = False
.Options.IgnoreUppercase = False

' Perform the spell checking
.ActiveDocument.CheckSpelling

' Select the new, corrected text
.Selection.WholeStory

' Copy Corrected text to Clipboard
.Selection.Copy

' strResults holds the text after the spell corrections
strResults = .Selection.Text

' close and free memory
.ActiveDocument.Close (0)
.Quit
End With

Set objWord = Nothing
' retrieve the corrected text from the clipboard
Text1.Text = Clipboard.GetText

End Sub