Solved

VB spell checker

Posted on 2000-03-28
38
573 Views
Last Modified: 2008-03-06
I am trying to implement a spell checker for a text box.  I have no clue where to even begin with this one.  Can someone point me in the right direction?!?  Thanks.
0
Comment
Question by:zbrusko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 13
  • 5
  • +4
38 Comments
 
LVL 1

Expert Comment

by:simonstre
ID: 2665097
use the evenment keyPress.  Copy each key pressed into a string. When u catch a space or ponctuation, check the string for a match into a dictionnary (contained in a database).  Have fun!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2665120
theres a spellecheck control you can get by going to Project|Components and scroll to the bottom.  I believe its called VCI VisualSpeller Library.  I dont know if this is only VB6 or not. However it is easy to implement, you can use it as such:

Private Sub Command1_Click()
    VSSpell.ClearCounts = True
    VSSpell.CheckText = txtBox.Text
    If VSSpell.ReplaceOccurred Then
        txtBox.Text = VSSpell.Text
    End If
End Sub

0
 
LVL 32

Expert Comment

by:bhess1
ID: 2665201
Here's a link to a spell check routine.  It works if you have MS Word only:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=3222
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 1

Expert Comment

by:jamauss
ID: 2665312
Use this fuction. It only works if you have some version of Word installed on the machine though. If your text is in a textbox called txtText1 Then you would call it like this:

txtText1.text = SpellCheck(txtText1.text)

Private Function SpellCheck(ByVal CheckText As String) As String

    Dim oWDBasic As Object
    Dim sTmpString As String
   
    Set oWDBasic = CreateObject("Word.Basic")
    oWDBasic.FileNew
    oWDBasic.Insert tbxComments.Text
   
    On Error Resume Next
   
    oWDBasic.ToolsSpelling
    oWDBasic.EditSelectAll
    oWDBasic.SetDocumentVar "MyVar", oWDBasic.Selection
    sTmpString = oWDBasic.GetDocumentVar("MyVar")
    tbxComments.Text = Left(sTmpString, Len(sTmpString) - 1)
    oWDBasic.FileClose 2
    oWDBasic.FileExit
   
    MsgBox "Spell Check is complete", vbOKOnly, "Done"

End Function

This will open an instance of word with the text you specify in it and spell check the text and then send it back to where it came from spell checked.
0
 

Author Comment

by:zbrusko
ID: 2665371
AzraSound,
    When I try to use the control (I have VB 5) you specified, I get the error "you do not have an appropriate lisence..."  What is that all about, and how can I get an appropriate license?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2665465
you probably dont have the license, it came with my version of VB6:
http://msdn.microsoft.com/vbasic/downloads/resguide/p19.asp
0
 
LVL 1

Expert Comment

by:jamauss
ID: 2665469
zbrusko,
   That probably means that the .ocx you are trying to use has either expired or isn't available to use with the version of VB you've got running. Does the code I provided work?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2665572
thats a steep price for that little ocx!!! How unfair, oh yeah, its MS.  
0
 

Author Comment

by:zbrusko
ID: 2667667
jamauss,
    your code didn't work either.  I got an error on "oWDBasic.Insert tbxComments.Text".  I tried that first, and have since tried a few other things, so I forget now what the exact error was.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2667978
From ZdNet:

If you plan to use the WebBrowser
control, there's no need to program Word for spell checking. That's because the WebBrowser control provides this functionality for you. To access it, you use the WebBrowser control's ExecWB() method with the OLECMDID_SPELL flag.

To illustrate, add the WebBrowser control to a form, then add the following code to the form's Load() event

WebBrowser1.Navigate "D:\Internet\spell.rtf"

Replace the path with any string that points to an RTF file.

Next, add a command button, and enter this code in its Click() event:

WebBrowser1.ExecWB OLECMDID_SPELL, OLECMDEXECOPT_DODEFAULT

Run the project. The WebBrowser displays the RTF file, and when you click the command button, VB runs through the normal spell check operation.
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2667998
From the http://support.microsoft.com/support/kb/articles/Q147/8/18.asp

I have tried to test and made a few modications....

1.Start a new project in Visual Basic. Form1 is created by default.

2.Add a text box (Text1) and CommandButton (Command1) to Form1.

3.Set the multi-line property of the text box to true.

4.Add the following code to the Form_Load event:

Private Sub Form_Load()
   Command1.Caption = "Press to SpellCheck"
   Text1.Text = "The Seattle Mariners ar goig all the wa this yeer!!"
End Sub

5.Place the following code in the Command1 Click event procedure of Form1:

Private Sub Command1_Click()
Dim oWDBasic As Object
Dim sTmpString As String
Set oWDBasic = CreateObject("Word.Basic")
oWDBasic.FileNew
oWDBasic.Insert Text1.Text
On Error Resume Next
oWDBasic.ToolsSpelling
oWDBasic.EditSelectAll
oWDBasic.SetDocumentVar "MyVar", oWDBasic.Selection
sTmpString = oWDBasic.GetDocumentVar("MyVar")
Text1.Text = Left(sTmpString, Len(sTmpString) - 1)
MsgBox "Spell Check is complete"
oWDBasic.FileClose 2
Set oWDBasic = Nothing
End Sub

6.Run the program.

Click the Command1 button, and go through the following sequence of corrections:

Mariners (Ignore)
ar(Change To are)
goig(Change To going)
wa(Change To way)
yeer(Change To Year)

After the last correction, you will get a Message box telling you that the spell check is complete. The results
inserted back into the Text1 text box should say this:

The Seattle Mariners are going all the way this year!!
0
 

Author Comment

by:zbrusko
ID: 2676583
That is great, but I have 2 problems with it.  For one, it opens Word, and keeps it open.  How can i close it?  Also, if I add the text:

The Flyers
are going
all the way
this year

it reformats it to:
"The|Flyers|are|going|all|the|way|this|year"

it doesn't keep my carriage returns, and it replaces all the spaces with "|".

How can I fix this?  Thanks.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2676596
to close Word, put this in a Module:

Declare Function EnumWindows Lib "user32" (ByVal wndenmprc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_CLOSE = &H10

Private Target As String
' Check a returned task to see if we should
' kill it.
Public Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long

    ' Get the window's title.
    length = GetWindowText(app_hWnd, buf, Len(buf))
    title = Left$(buf, length)

    ' See if this is the target window.
    If InStr(title, Target) <> 0 Then
        ' Kill the window.
        SendMessage app_hWnd, WM_CLOSE, 0, 0
    End If
   
    ' Continue searching.
    EnumCallback = 1
End Function

' Ask Windows for the list of tasks.
Public Sub TerminateTask(app_name As String)
    Target = app_name
    EnumWindows AddressOf EnumCallback, 0
End Sub



Then you can call it like this:

TerminateTask "Word"


0
 
LVL 1

Expert Comment

by:jamauss
ID: 2676692
zbrusko:
   Why have you not tried what code I put up there for you? All the problems you are finding could be solved if you just used the code I supplied you earlier. It automatically closes word, replaces the text just fine and everything. Have you even tried out the code I posted for you?
0
 

Author Comment

by:zbrusko
ID: 2676801
Jamauss,
    I am getting an error with your code on this line:    
oWDBasic.Insert tbxComments.Text

"object required"
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2676813
you may want to look at this free spell check control offered at vbweb:
http://www.vbweb.co.uk/dev/index.htm?spell_checker.htm
0
 
LVL 1

Expert Comment

by:jamauss
ID: 2676857
Yeah, because "tbxComments" is the name of the control I used for this, you need to use the name of the textbox control that YOU are using, not mine.

the "object required" message is telling you. "Hey, I don't see any tbxComments control!" so you need to change it to be txtText or whatever the name of the control you're checking the text of is.

-Jason
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2677269
Jason.. his textboxes are fine.. take another look at your code and explain this line.. <smile>.

Set oWDBasic = CreateObject("Word.Basic")
0
 
LVL 1

Expert Comment

by:jamauss
ID: 2677399
If you have Word installed on your computer then "Word.Basic" should be a distributed component that you should be able to reference just like any other COM component written in VB.If you have Visual Studio 6 tools installed, you can use the OLE viewer tool to determine if Word.Basic in on your machine. oWDBasic is just an object variable being set to a new word document. Word.Basic is referencing the component to start up the word document. I guess the Word.Basic isn't on the machine but what error are you getting trying my code? It shouldn't be an object required error. Also, you can try checking your references in your project also. That might be the problem also.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2677802
it must be on the machine cuz Ruchi's code worked
0
 

Author Comment

by:zbrusko
ID: 2677842
This is what I have...

Private Sub cmdSpell_Click()
    txtMessages.Text = SpellCheck(txtMessages.Text)
End SubPrivate Function SpellCheck(ByVal CheckText As String) As String
    Dim oWDBasic As Object
    Dim sTmpString As String
   
    Set oWDBasic = CreateObject("Word.Basic")
    oWDBasic.FileNew
    oWDBasic.Insert txtMessages.Text
    On Error Resume Next
    oWDBasic.ToolsSpelling
    oWDBasic.EditSelectAll
    oWDBasic.SetDocumentVar "MyVar", oWDBasic.Selection
    sTmpString = oWDBasic.GetDocumentVar("MyVar")
    txtMessages.Text = Left(sTmpString, Len(sTmpString) - 1)
    oWDBasic.FileClose 2
    oWDBasic.FileExit
    MsgBox "Spell Check is complete", vbOKOnly, "Done"
End Function

with the following input:
The Flyers
are the bist team.

I get (after spell checking):
The Flyers|are the best team.

But when I click "ok" on the message box, all my text disappears!

As you probably know, I am still new at VB, so maybe I am missing something obvious.  Thanks a lot.
0
 

Author Comment

by:zbrusko
ID: 2677845
oops, I do have a carriage return after "end sub" and before "private function..."  stupid cut and paste...
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2677861
oWDBasic.Insert txtMessages.Text
   
Change txtMessages.Text to CheckText
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2677863
txtMessages.Text = Left(sTmpString, Len(sTmpString) - 1)
   

change it there as well
0
 

Author Comment

by:zbrusko
ID: 2677962
Nope, that didn't do anything...
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2678008
it seems the carriage returns are your only difficulty.  So I have provided the workaround.  Here it is:

Option Explicit
Dim temp As String
Dim arr() As String

Private Sub Command1_Click()
    Dim oWDBasic As Object
    Dim sTmpString As String
    Dim i As Integer
    Dim j As Integer
    j = 0
   
    arr = Split(txtMessages.Text, vbCrLf)
    i = UBound(arr)
    Text2.Text = i
    Set oWDBasic = CreateObject("Word.Basic")
    oWDBasic.FileNew
   
    Do
    oWDBasic.Insert arr(j)
    On Error Resume Next
    oWDBasic.ToolsSpelling
    oWDBasic.EditSelectAll
    oWDBasic.SetDocumentVar "MyVar", oWDBasic.Selection
    sTmpString = oWDBasic.GetDocumentVar("MyVar")
    arr(j) = Left(sTmpString, Len(sTmpString) - 1)
    j = j + 1
    Loop Until j = i + 1
   
    oWDBasic.FileClose 2
    oWDBasic.FileExit
    MsgBox "Spell Check is complete", vbOKOnly, "Done"
   
    txtMessages.Text = Join(arr, vbCrLf)
End Sub


0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2678010
dont mind that Text2.Text = i line, that was just a verification check on my part
0
 

Author Comment

by:zbrusko
ID: 2678028
Where does the Split function come from?  It says that it can't be found.  Thanks.
0
 

Author Comment

by:zbrusko
ID: 2678031
Adjusted points from 100 to 200
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2678035
it is a builtin VB function...unless your version doesnt have that either
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2678044
what it does is split a string into an array based on the delimiter parameter passed to it.  In your case, vbCrLf means to split the string into elements of an array at all the carriage returns.  So element 0 of the array holds all the text up to the first carriage return, element 1 of the array the next string of text to the second carriage return and so on.  Basically in this case it will store each line of text as a separate element into the array.  It is a very powerful function and without it would mean implementing it yourself which will be more tedious.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2679080
Hello zbrusko, I wrote out the Split function for you, hopefully it runs now:

Dim temp As String
Dim arr() As String
Dim k As Integer

Private Sub Command1_Click()
    Dim oWDBasic As Object
    Dim sTmpString As String
    Dim i As Integer
    Dim j As Integer


    arr = SplitFunc(txtMessages.Text, vbCrLf)
    i = UBound(arr)
    Set oWDBasic = CreateObject("Word.Basic")
    oWDBasic.FileNew
   
    Do
    oWDBasic.Insert arr(j)
    On Error Resume Next
    oWDBasic.ToolsSpelling
    oWDBasic.EditSelectAll
    oWDBasic.SetDocumentVar "MyVar", oWDBasic.Selection
    sTmpString = oWDBasic.GetDocumentVar("MyVar")
    arr(j) = Left(sTmpString, Len(sTmpString) - 1)
    j = j + 1
    Loop Until j = i + 1

    oWDBasic.FileClose 2
    oWDBasic.FileExit
    MsgBox "Spell Check is complete", vbOKOnly, "Done"

    txtMessages.Text = Join(arr, vbCrLf)
End Sub

Private Function SplitFunc(Expression As String, Optional Delimiter As String) As Variant
    Dim i As Long
    Dim lNextPos As Long
    Dim sText As String
    Dim lCount As Long
    Dim varTemp() As String
    For i = 1 To Len(Expression)
       
        lNextPos = InStr(i + 1, Expression, Delimiter)
        If lNextPos = 0 Then
            lNextPos = Len(Expression)
        End If
        sText = Mid$(Expression, i, lNextPos - i)
        ReDim Preserve varTemp(lCount)
        varTemp(lCount) = sText
        lCount = lCount + 1
        i = lNextPos + Len(Delimiter) - 1
       
    Next
    SplitFunc = varTemp
End Function
0
 

Author Comment

by:zbrusko
ID: 2680461
It gives me a "can't assign to array" error on "arr = SplitFunc(txtMessages.Text, vbCrLf)".  Any ideas?

Option Explicit
Dim temp As String
Dim arr() As String
Dim k As Integer

Private Sub cmdSpell_Click()
   Dim oWDBasic As Object
    Dim sTmpString As String
    Dim i As Integer
    Dim j As Integer
   
    j = 0
    arr = SplitFunc(txtMessages.Text, vbCrLf)
    i = UBound(arr)
    Set oWDBasic = CreateObject("Word.Basic")
    oWDBasic.FileNew
    Do
        oWDBasic.Insert arr(j)
        On Error Resume Next
        oWDBasic.ToolsSpelling
        oWDBasic.EditSelectAll
        oWDBasic.SetDocumentVar "MyVar", oWDBasic.Selection
        sTmpString = oWDBasic.GetDocumentVar("MyVar")
        arr(j) = Left(sTmpString, Len(sTmpString) - 1)
        j = j + 1
    Loop Until j = i + 1
    oWDBasic.FileClose 2
    oWDBasic.FileExit
    MsgBox "Spell Check is complete", vbOKOnly, "Done"
    txtMessages.Text = Join(arr, vbCrLf)
End Sub

Private Function SplitFunc(Expression As String, Optional Delimiter As String) As Variant
    Dim i As Long
    Dim lNextPos As Long
    Dim sText As String
    Dim lCount As Long
    Dim varTemp() As String
    For i = 1 To Len(Expression)
        lNextPos = InStr(i + 1, Expression, Delimiter)
        If lNextPos = 0 Then
            lNextPos = Len(Expression)
        End If
        sText = Mid$(Expression, i, lNextPos - i)
        ReDim Preserve varTemp(lCount)
        varTemp(lCount) = sText
        lCount = lCount + 1
        i = lNextPos + Len(Delimiter) - 1
    Next
    SplitFunc = varTemp
End Function
0
 

Author Comment

by:zbrusko
ID: 2680463
Adjusted points from 200 to 250
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2680573
change
Dim arr() as String
to
Dim arr as Variant

and this code should work.


You could also try:

Dim arr() as String
Dim vArr as Variant
....
    vArr = SplitFunc(txtMessages.Text, vbCrLf)
    arr = vArr

Notes - The Split function is only available in VB6 or later

Returning arrays only works between variants.

If this works, don't give me the points - I only did clean up.  Others did the real work.
0
 

Author Comment

by:zbrusko
ID: 2680984
Well, it looks like Join doesn't want to work either...
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 250 total points
ID: 2681101
Private Function JoinFunc(Arr As Variant, Optional Delimiter As String) As String
    Dim i As Integer
    Dim tmpString As String
    If Delimiter = "" Then Delimiter = " "
    For i = 0 To UBound(Arr)
        tmpString = tmpString & Arr(i) & Delimiter
    Next
    JoinFunc = tmpString
End Function

Call this JoinFunc instead
0
 

Author Comment

by:zbrusko
ID: 2681161
Thanks a lot!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question