Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Spell Check in VB App 4

Status
Not open for further replies.

samanta

Programmer
May 29, 2001
1
US
How do I do a spellcheck on text field in VB
 
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.

Herman :-Q
 
Well, Herman, I tried your code, but I get Object variable not set on line "wordApp.Selection.Text = " Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
hi Tim.
Didn't you forgot to declare the wordapp object?
You need to add Dim wordapp as object to the code.

Also if this isn't the case you can also try to do:

With wordapp
Selection.text= "Your text here"

Instead of wordapp.selection.text= "Your text here"

Hope this helps.
 
Daimaou is right. I left out the dim statements, as I thought the essential code would be sufficient. I'll be more complete next time. Sorry.

Herman :-Q
 
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

wdApp.Height = 200
wdApp.Width = 300
wdApp.Left = 50

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(&quot;The length of the corrected text has acceeded the allowed maximum.&quot; & Chr(13) & &quot;Truncate and continue? Click cancel to keep original text.&quot;, 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 = &quot;English&quot; Then
wdApp.Selection.LanguageID = wdEnglishUS
Else
wdApp.Selection.LanguageID = wdDutch
End If

nRet = wdApp.Dialogs(wdDialogToolsSpellingAndGrammar).Show

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(, &quot;Word.Application&quot;)
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(, &quot;Word.Application&quot;)
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.

Herman :-Q
 
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([cry]). 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. [curse]

Oh well, that's nothing new from this guy.

John [spidey] [americanflag] [unclesam]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top