• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 580
  • Last Modified:

VB spell checker

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
zbrusko
Asked:
zbrusko
  • 14
  • 13
  • 5
  • +4
1 Solution
 
simonstreCommented:
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
 
AzraSoundCommented:
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
 
Brendt HessSenior DBACommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jamaussCommented:
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
 
zbruskoAuthor Commented:
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
 
AzraSoundCommented:
you probably dont have the license, it came with my version of VB6:
http://msdn.microsoft.com/vbasic/downloads/resguide/p19.asp
0
 
jamaussCommented:
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
 
AzraSoundCommented:
thats a steep price for that little ocx!!! How unfair, oh yeah, its MS.  
0
 
zbruskoAuthor Commented:
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
 
wsh2Commented:
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
 
RuchiCommented:
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
 
zbruskoAuthor Commented:
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
 
AzraSoundCommented:
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
 
jamaussCommented:
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
 
zbruskoAuthor Commented:
Jamauss,
    I am getting an error with your code on this line:    
oWDBasic.Insert tbxComments.Text

"object required"
0
 
AzraSoundCommented:
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
 
jamaussCommented:
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
 
wsh2Commented:
Jason.. his textboxes are fine.. take another look at your code and explain this line.. <smile>.

Set oWDBasic = CreateObject("Word.Basic")
0
 
jamaussCommented:
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
 
AzraSoundCommented:
it must be on the machine cuz Ruchi's code worked
0
 
zbruskoAuthor Commented:
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
 
zbruskoAuthor Commented:
oops, I do have a carriage return after "end sub" and before "private function..."  stupid cut and paste...
0
 
AzraSoundCommented:
oWDBasic.Insert txtMessages.Text
   
Change txtMessages.Text to CheckText
0
 
AzraSoundCommented:
txtMessages.Text = Left(sTmpString, Len(sTmpString) - 1)
   

change it there as well
0
 
zbruskoAuthor Commented:
Nope, that didn't do anything...
0
 
AzraSoundCommented:
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
 
AzraSoundCommented:
dont mind that Text2.Text = i line, that was just a verification check on my part
0
 
zbruskoAuthor Commented:
Where does the Split function come from?  It says that it can't be found.  Thanks.
0
 
zbruskoAuthor Commented:
Adjusted points from 100 to 200
0
 
AzraSoundCommented:
it is a builtin VB function...unless your version doesnt have that either
0
 
AzraSoundCommented:
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
 
AzraSoundCommented:
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
 
zbruskoAuthor Commented:
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
 
zbruskoAuthor Commented:
Adjusted points from 200 to 250
0
 
Brendt HessSenior DBACommented:
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
 
zbruskoAuthor Commented:
Well, it looks like Join doesn't want to work either...
0
 
AzraSoundCommented:
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
 
zbruskoAuthor Commented:
Thanks a lot!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 14
  • 13
  • 5
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now