MSWord has a good spellchecker. It's easy to activate from VB. What I did:
set wordApp = createObject("Word.Application"
wordApp.Selection.Text = TextToCheck
nRet = wordApp.Dialogs(wdDialogToolsSpellingAndGrammar).Show
SpellIt = wordApp.Selection.Document.Content
SpellIt = Left(SpellIt, Len(SpellIt) - 1)
'
'some strings may contain chr(13) followed by chr(10) (= Ctrl+Return)
'Word deletes the chr(10) characters
'which gives not the proper characters in the textboxes
'so add the characters that have been deleted.
i = InStr(SpellIt, Chr(13))
Do While i > 0
SpellIt = Left(SpellIt, i) & Chr(10) & Mid(SpellIt, i + 1)
i = InStr(i + 1, SpellIt, Chr(13))
Loop
You could also activate the spellchecker for different languages.
One thing that I encountered is that corrected strings may grow because the spellchecker might replace words with longer ones.
tried it all sorts of ways: (some still there but commented out, and I have added a reference to Word in my project)
Dim wordApp As New Word.Application
'Set wordApp = New Word.Application 'CreateObject("Word.Application"
Set wordApp.Selection.Text = "Gorftyu" 'Text1.Text
nRet = wordApp.Dialogs(wdDialogToolsSpellingAndGrammar).Show
SpellIt = wordApp.Selection.Document.Content
SpellIt = Left(SpellIt, Len(SpellIt) - 1)
'
'some strings may contain chr(13) followed by chr(10) (= Ctrl+Return)
'Word deletes the chr(10) characters
'which gives not the proper characters in the textboxes
'so add the characters that have been deleted.
i = InStr(SpellIt, Chr(13))
Do While i > 0
SpellIt = Left(SpellIt, i) & Chr(10) & Mid(SpellIt, i + 1)
i = InStr(i + 1, SpellIt, Chr(13))
Loop
Tim
Remember the KISS principle:
Keep It Simple, Stupid!
Ok, I made a sample form and a BAS module. On the form I have a textbox and a commandbutton. When the button is clicked, then the text in the textbox is checked with Word's spellchecker. Note: Word does only display its dialog box if it find errors in the text.
First the code in the form:
Option Explicit
Private Sub Command1_Click()
Dim checkText As String
checkText = Text1.text
If fnWordActive Then
MsgBox "Close other MSWord instances"
Exit Sub
Else
Text1.text = fnSpellCheck(checkText, 500, "English"
End If
'don't forget to cleanup
CleanUpWord
End Sub
Now the code in the bas module:
Option Explicit
Private wdApp As Word.Application
Private wdDoc As Word.Document
Private wdOrig As Word.Application
Private wdRunning As Boolean
Private checkText As String
Private checkLanguage As String
Global Const sEmpty = ""
Public Function fnWord() As Boolean
On Error Resume Next 'ignore errors
fnWord = True
If Not wdRunning Then
'A Word instance is active. This can only occur if the user
'has activated the spellingchecker from the frmSpellCheck
'(Checking the Requirement texts).
wdRunning = True
Set wdApp = New Word.Application 'run it
If Err.Number <> 0 Then
fnWord = False
Else
Set wdDoc = wdApp.Documents.Add
End If
End If
Err.Clear ' Clear Err object in case error occurred.
On Error GoTo 0 'Resume normal error processing
End Function
Public Function fnSpellCheck(text As String, Optional maxlength As Integer, Optional sLanguage As String)
Dim c As Integer
On Error Resume Next
Screen.MousePointer = vbHourglass
checkText = text
checkLanguage = sLanguage
fnSpellCheck = SpellIt
If maxlength > 0 Then
If Len(fnSpellCheck) > maxlength Then
c = MsgBox("The length of the corrected text has acceeded the allowed maximum." & Chr(13) & "Truncate and continue? Click cancel to keep original text.", vbOKCancel + vbDefaultButton1)
If c = vbOK Then
fnSpellCheck = Left(fnSpellCheck, 255)
Else
fnSpellCheck = text
End If
End If
End If
Screen.MousePointer = vbDefault
Exit Function
MsgErr:
Resume Next
End Function
Private Function SpellIt()
Dim nRet As Long
Dim i As Integer
On Error GoTo ErrHandler
SpellIt = checkText
'Check if Word is active, if not run it
If Not fnWord Then Exit Function 'if it creates an error, exit
wdApp.Selection.Document.Content = sEmpty
wdApp.Selection.text = checkText
If checkLanguage = sEmpty Or checkLanguage = "English" Then
wdApp.Selection.LanguageID = wdEnglishUS
Else
wdApp.Selection.LanguageID = wdDutch
End If
SpellIt = wdApp.Selection.Document.Content
SpellIt = Left(SpellIt, Len(SpellIt) - 1)
'
'some strings may contain chr(13) followed by chr(10) (= Ctrl+Return)
'Word deletes the chr(10) characters
'which gives not the proper characters in the textboxes
'so add the characters that have been deleted.
i = InStr(SpellIt, Chr(13))
Do While i > 0
SpellIt = Left(SpellIt, i) & Chr(10) & Mid(SpellIt, i + 1)
i = InStr(i + 1, SpellIt, Chr(13))
Loop
Exit Function
ErrHandler:
Resume Next
End Function
Public Sub CleanUpWord()
On Error GoTo MsgErr
wdRunning = False
RestoreWdSettings
wdApp.Documents.Close wdDoNotSaveChanges
wdApp.Quit
Set wdApp = Nothing
On Error Resume Next
Set wdOrig = GetObject(, "Word.Application"
If Err = 0 Then
wdOrig.WindowState = wdWindowStateMaximize
Set wdOrig = Nothing
End If
Exit Sub
MsgErr:
Resume Next
End Sub
Private Sub RestoreWdSettings()
With wdApp
.WindowState = wdWindowStateMaximize
End With
End Sub
Public Function fnWordActive() As Boolean
On Error Resume Next
Set wdOrig = GetObject(, "Word.Application"
If Err = 0 Then
fnWordActive = True
Else
fnWordActive = False
End If
Set wdOrig = Nothing
End Function
I removed the messages in the errorhandler. However it would be wise to display some message it an error occurred.
Believe it or not the Microsoft Knowledge Base has a good way to spell check & it's pretty simple. Follow the instructions and in less than 5 minutes you can be using an instance of word to check spelling!
I have a freeware Spell Checker OCX that I downloaded somewhere on the internet that works great. Plus, it doesn't requuire the target machine to have Word installed. As soon as I can find the URL again, I will post it.
I tried that one before I went to the Knowledge Base(). I had some problems with it. It came with a dictionary of 8500 words. Someone else there had a dictionary of 175000 words. I got both of them and pulled the discionary out of a VFP spell checker we have here and I managed to get the dictionary up to 375000 words. Unfortunately on my 2 Ghz machine, it would take a minute for the dictionary to load. The users would be lucky to have 600 Mhx machines. If they were lucky they could spell check once a day. LOL My original idea was to not use Word if I did not have to, but it was giving me some probs (especially on the fields the user wants in upper case).
So I was forced to go to Word, 'cause I need this is the project ASAP!!! The user waited until after the project went live to decide that his employees could not type to save their lives.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.