# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  VB - Spell Check a Textbox

## MartinLiss

Add this code to a command button or popup menu item.


VB Code:
Dim objWord As Object
    Dim objDoc  As Object
    Dim strResult As String
    
    'Create a new instance of word Application
    Set objWord = CreateObject("word.Application")
     Select Case objWord.Version
        'Office 2000
        Case "9.0"
            Set objDoc = objWord.Documents.Add(, , 1, True)
        'Office XP
        Case "10.0"
            Set objDoc = objWord.Documents.Add(, , 1, True)
        'Office 97
        Case Else ' Office 97
            Set objDoc = objWord.Documents.Add
    End Select
     objDoc.Content = Text1.Text
    objDoc.CheckSpelling
     strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
     If Text1.Text = strResult Then
        ' There were no spelling errors, so give the user a
        ' visual signal that something happened
        MsgBox "The spelling check is complete.", vbInformation + vbOKOnly
    End If
    
    'Clean up
    objDoc.Close False
    Set objDoc = Nothing
    objWord.Application.Quit True
    Set objWord = Nothing
     ' Replace the selected text with the corrected text. It's important that
    ' this be done after the "Clean Up" because otherwise there are problems
    ' with the screen not repainting
    Text1.Text = strResult
     Exit Sub

----------


## The Hobo

I'm guessing Word has to be installed for this to work?

----------


## crptcblade

Yup

----------


## The Hobo

Bugger  :Frown:

----------


## bassist695

> strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)


How does this translate into VB.net? Any ideas? It's the one line that's not compatible in our app. The Left function is considered an integer...If you could just let me know what this does (dump contents of Word doc into strResult?), I can probably figure out how to tweak it.

Thanks,
mike

----------


## crptcblade

VB Code:
strResult = objDoc.Content.Substring(0,objDoc.Content.Length-1)

Probably something like that.

----------


## Tormentlord

So wut your saying here, is that if I have a text game I can spell check the commands entered by the user? accordingly to the command?

----------


## MartinLiss

If you have Word on your PC and you add the code to a textbox in your game, the textbox will will behave the same way that Word does when you press F7.

----------


## sv_bhaskar

hi

when spell check window is open and if you try to switch focus to some other windows application, a msg box appears with message "This action cannot be completed because other application is busy. Choose 'Switch To' to activate the busy application and correct the problem."

how to get rid of this message window and display the spell check window as modal window?

thanks in advance.

----------


## MartinLiss

Add the following before the word object is created.


    App.OleRequestPendingTimeout = 999999

----------


## Mark Gambo

Marty,
           Today at work we were playing around with your code in order to see if we could spell check and check the grammar of the data contained in the Text box. We changed the following snippet:


VB Code:
objDoc.CheckSpelling

to


VB Code:
objDoc.CheckGrammar

and viola it worked! But you probably already knew this but I think the other forum members might be able to intergrate this into their projects.

Do you know a way this code could be modified to work with Wordperfect? :wave:   :Cool:   :Big Grin:   :Smilie:

----------


## MartinLiss

If Wordperfect has an object module that exposes the right properties then I guess so, but I've no experience with it.

----------


## Madboy

Whats WordPerfect?

----------


## MartinLiss

> _Originally posted by Madboy_ 
> *Whats WordPerfect?*


 http://www.corel.com/servlet/Satelli...=1042153063297

----------


## Madboy

Sounds good, does anybody use it?

----------


## MartinLiss

You really are young. WordPerfect was _the_ word-processing program before MS Word came along. I'm sure many, many people still use it, but no doubt less than Word.

----------


## Madboy

hehehe, i didnt realise. Im only 17 so there you go, didnt realise such a thing existed.

Well, you learn something new everyday :Stick Out Tongue:

----------


## rike

Martin I am trying to use the above code with numerous textboxes on the same form and have altered it to achieve this.  The problem I am having is if I cancel the spellchecker it proceeds to the next textbox and opens again.  Is there any way I can capture the return value of the spellchecker so if it is canceled I can exit the sub

----------


## MartinLiss

Please post your code; at least the cmdSpell_Click() sub.

----------


## rike

Basically a company using access 97 runtime with office 2003 want to see if they can use the 2003 spell checker as the 97 office spellchecker is no longer installed.  So I want to use this code in an access form.

I was hoping to loop through all the controls on the form but first altered your code slightly to see how it would handle two text boxes.


VB Code:
Private Sub Command0_Click()
    Dim objWord As Object
    Dim objDoc1, ojbDoc2  As Object
    Dim strResult1, strResult2 As String
    
    'Create a new instance of word Application
    Set objWord = CreateObject("word.Application")
     Select Case objWord.Version
        'Office 2000
        Case "9.0"
            Set objDoc = objWord.Documents.Add(, , 1, True)
         'Office XP
        Case "10.0", "11.0"
            Set objDoc1 = objWord.Documents.Add(, , 1, True)
            Set objDoc2 = objWord.Documents.Add(, , 1, True)
                   
        'Office 97
        Case Else ' Office 97
            Set objDoc = objWord.Documents.Add
    End Select
     Me.Text1.SetFocus
    objDoc1.Content = Text1.Text
    objDoc1.CheckSpelling
     strResult1 = Left(objDoc1.Content, Len(objDoc1.Content) - 1)
    
    
    
    Me.Text2.SetFocus
    objDoc2.Content = Text2.Text
    objDoc2.CheckSpelling
      strResult2 = Left(objDoc2.Content, Len(objDoc2.Content) - 1)
     If Text2.Text = strResult2 Then
        Text1.SetFocus
        If Text1.Text = strResult1 Then
            ' There were no spelling errors, so give the user a
            ' visual signal that something happened
            MsgBox "The spelling check is complete.", vbInformation + vbOKOnly
        End If
    End If
    
    'Clean up
    objDoc1.Close False
    objDoc2.Close False
    Set objDoc1 = Nothing
    Set objDoc2 = Nothing
    objWord.Application.Quit True
    Set objWord = Nothing
     ' Replace the selected text with the corrected text. It's important that
    ' this be done after the "Clean Up" because otherwise there are problems
    ' with the screen not repainting
    Text1.SetFocus
    Text1.Text = strResult1
    Text2.SetFocus
    Text2.Text = strResult2
     Exit Sub
End Sub

How would you approach this?

----------


## MartinLiss

VB Code:
Private Sub Command0_Click()
    Dim objWord As Object
    Dim objDoc  As Object
    Dim strResult As String
    Dim ctl As Control
    
    App.OleRequestPendingTimeout = 999999
    'Create a new instance of word Application
    Set objWord = CreateObject("word.Application")
     Select Case objWord.Version
        'Office 2000
        Case "9.0"
            Set objDoc = objWord.Documents.Add(, , 1, True)
        'Office XP
        Case "10.0"
            Set objDoc = objWord.Documents.Add(, , 1, True)
        'Office 97
        Case Else ' Office 97
            Set objDoc = objWord.Documents.Add
    End Select
    
    For Each ctl In Controls
        If TypeOf ctl Is TextBox Then
            objDoc.Content = ctl.Text
            objDoc.CheckSpelling
        
            strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
        
            If ctl.Text = strResult Then
                ' There were no spelling errors, so give the user a
                ' visual signal that something happened
                MsgBox "The spelling checking for " & ctl.Name & " is complete.", vbInformation + vbOKOnly
            End If
            
        End If
    Next
    'Clean up
    objDoc.Close False
    Set objDoc = Nothing
    objWord.Application.Quit True
    Set objWord = Nothing
     ' Replace the selected text with the corrected text. It's important that
    ' this be done after the "Clean Up" because otherwise there are problems
    ' with the screen not repainting
    Text1.Text = strResult
 End Sub

BTW it's a common mistake but when things are defined the way
you did it in *Dim objDoc1, ojbDoc2  As Object* only objDoc2 is an Object, objDoc1 is left as a Variant.

----------


## rike

Cheers for the tip

App.OleRequestPendingTimeout = 999999

is not recognised in Access is this line necessary.  I tried commenting it out and alter the code as such for access but still the word doc comes up if I cancel the spellchecker during the check.


VB Code:
Private Sub Command0_Click()
    Dim objWord As Object
    Dim objDoc  As Object
    Dim strResult As String
    Dim ctl As Control
    
    
    'Create a new instance of word Application
    Set objWord = CreateObject("word.Application")
     Select Case objWord.Version
        'Office 2000
        Case "9.0"
            Set objDoc = objWord.Documents.Add(, , 1, True)
        'Office XP
        Case "10.0", "11.0"
            Set objDoc = objWord.Documents.Add(, , 1, True)
        'Office 97
        Case Else ' Office 97
            Set objDoc = objWord.Documents.Add
    End Select
    
    For Each ctl In Controls
        If TypeOf ctl Is TextBox Then
            ctl.SetFocus
            objDoc.Content = ctl.Text
            objDoc.CheckSpelling
        
            strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
        
            If ctl.Text = strResult Then
                ' There were no spelling errors, so give the user a
                ' visual signal that something happened
                MsgBox "The spelling checking for " & ctl.Name & " is complete.", vbInformation + vbOKOnly
            End If
            
        End If
    Next
    'Clean up
    objDoc.Close False
    Set objDoc = Nothing
    objWord.Application.Quit True
    Set objWord = Nothing
     ' Replace the selected text with the corrected text. It's important that
    ' this be done after the "Clean Up" because otherwise there are problems
    ' with the screen not repainting
    Text1.SetFocus
    Text1.Text = strResult
 End Sub

Is this because I have ommitted 'App.OleRequestPendingTimeout = 999999' ?

----------


## MartinLiss

The *App.OleRequestPendingTimeout = 999999* came from the problem that sv_bhaskar pointed out above and you may not need it.

Try *objWord.Visible = False* for your problem.

----------


## rike

No that didn't work either.  I guess I will just force them to check only one text box at a time.  

Cheers for your help.

----------


## MartinLiss

I may not be able to do anything about it but would you like to send me your project?

----------


## rike

I haven't added it to my project yet as I was just investigating the possibility.  I just created a form in Access with a couple of textboxes and put your code under a command button.  Unfortunately problems arose when cancelling the spellchecker.

----------


## MartinLiss

How about attaching that project or creating a new one that demonstrates the problem.

----------


## MartinLiss

Three problems have been reported by rike when using my code in VBA. Those problems are:

1. Text boxes that contain carriage returns are displayed as non recognisable character formats after spell checking

2.When the spell checker window is moved, the screen is not repainted

3 When users try to use other applications while the spellchecker is in progress the screen gets confused and appears to crash.

I don't program in VBA but I have been able to verify that problem 1 occurs in VB as well. The following is a fix for that problem. (Add the bolded line to the existing code).


VB Code:
objDoc.CheckSpelling
     strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
    ' Correct the carriage returns
    [b]strResult = Replace(strResult, Chr(13), Chr(13) & Chr(10))[/b]
    
    If Text1.Text = strResult Then
        ' There were no spelling errors, so give the user a
        ' visual signal that something happened
        MsgBox "The spelling check is complete.", vbInformation + vbOKOnly
    End If

----------


## RudyL

> _Originally posted by MartinLiss_ 
> 
> VB Code:
> objDoc.CheckSpelling
>      strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
>     ' Correct the carriage returns
>     [b]strResult = Replace(strResult, Chr(13), Chr(13) & Chr(10))<b>Three problems have been reported by rike when using my code in VBA. Those problems are:
>  1. Text boxes that contain carriage returns are displayed as non recognisable character formats after spell checking
>  2.When the spell checker window is moved, the screen is not repainted
> ...


Martin,

Nice code.. One question though.. I get a screen flicker and see a word document displayed on my screen for a brief second.. Any ideas as to why?

----------


## jhermiz

> _Originally posted by RudyL_ 
> *Martin,
> 
> Nice code.. One question though.. I get a screen flicker and see a word document displayed on my screen for a brief second.. Any ideas as to why?*


objWord.Visible=False

----------


## RudyL

> _Originally posted by jhermiz_ 
> *objWord.Visible=False*


That does not work. In fact, if I click on cancel the word document does not close at all..  wierd..

----------


## jhermiz

> _Originally posted by RudyL_ 
> *That does not work. In fact, if I click on cancel the word document does not close at all..  wierd..*


How does it not work?  It sets the actual program (word) as invisible.  You shouldnt see word at all.  It should be placed right after you create the instance of word.

----------


## RudyL

> _Originally posted by jhermiz_ 
> *How does it not work?  It sets the actual program (word) as invisible.  You shouldnt see word at all.  It should be placed right after you create the instance of word.*


I still get a flicker, or flash of the word object after the ok button is clicked, as it is closing the object.. It isn't major, but it is anoying. The new problem I posted is a much bigger problem. Not sure why it did that.

----------


## MartinLiss

Apparently I never tested clicking the Cancel button. Anyhow with the program the way it is now, if you click Cancel the Word doc will show up and stay open, but only until you click the OK button on the _The spelling check is complete._ MsgBox. That behavior can be corrected by adding an *objWord.Visible = False* line. Where you place that line is important.  


VB Code:
objDoc.Content = Text1.Text
    objDoc.CheckSpelling
    objWord.Visible = False

BTW I assume you see a "flash" because you have a slow PC.

----------


## RudyL

> _Originally posted by MartinLiss_ 
> *Apparently I never tested clicking the Cancel button. Anyhow with the program the way it is now, if you click Cancel the Word doc will show up and stay open, but only until you click the OK button on the The spelling check is complete. MsgBox. That behavior can be corrected by adding an objWord.Visible = False line. Where you place that line is important.  
> 
> 
> VB Code:
> objDoc.Content = Text1.Text
>     objDoc.CheckSpelling
>     objWord.Visible = False
> 
> BTW I assume you see a "flash" because you have a slow PC.*


Ok, I had the .visible = flase in the wrong place. That fixed the ignore thing..

The flashing part though.. You almost hade me on the speed of my pc except that I have a Pentium 2.6 gig processor so I do not think that is the culprit..   :Big Grin:

----------


## jhermiz

> _Originally posted by MartinLiss_ 
> *Add this code to a command button or popup menu item.
> 
> 
> VB Code:
> Dim objWord As Object
>     Dim objDoc  As Object
>     Dim strResult As String
>     
> ...


Why dont you make this a function and pass a text box control?

What's the latest and greatest code ?

----------


## MartinLiss

> _Originally posted by jhermiz_ 
> *Why dont you make this a function and pass a text box control?*


 Be my guest.




> _Originally posted by jhermiz_ 
> *What's the latest and greatest code ?*


 Don't know about "greatest" but that's the latest.

----------


## jhermiz

> _Originally posted by MartinLiss_ 
> *Be my guest.
> 
>  Don't know about "greatest" but that's the latest.*



Marty can you help here:

http://www.vbforums.com/showthread.p...hreadid=295968

----------


## jhermiz

For anyone interested..

I have taken marty's subroutine and converted it to a generic function taking in a text box as a parameter.

Here is the code if anyone is interested:


VB Code:
Public Function CheckSpelling(t As TextBox)
On Error GoTo Err_Handler
     Dim objWord As Object
    Dim objDoc  As Object
    Dim strResult As String
    
    'Create a new instance of word Application
    If (Len(t.Text) = 0) Then
        'nahhhhhhhhhhh
    Else
        App.OleRequestPendingTimeout = 999999
        Set objWord = CreateObject("word.Application")
        objWord.Visible = False
        
        Select Case objWord.Version
            'Office 2000, xp, 2k3
                Case "9.0", "10.0", "11.0"
                Set objDoc = objWord.Documents.Add(, , 1, True)
            'Office 97
                Case Else
                Set objDoc = objWord.Documents.Add
        End Select
         objDoc.Content = t.Text
        objDoc.CheckSpelling
        objWord.Visible = False
        
        strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
        'correct the carriage returns
        strResult = Replace(strResult, Chr(13), Chr(13) & Chr(10))
         If t.Text = strResult Then
            ' There were no spelling errors, so give the user a
            ' visual signal that something happened
            MsgBox "The spelling check is complete.", vbInformation + vbOKOnly, "Spelling Complete"
        End If
    
        'Clean up
        objDoc.Close False
        Set objDoc = Nothing
        objWord.Application.Quit True
        Set objWord = Nothing
         ' Replace the selected text with the corrected text. It's important that
        ' this be done after the "Clean Up" because otherwise there are problems
        ' with the screen not repainting
        t.Text = strResult
    End If
 Done:
Exit Function
 'in case user does not have word...
Err_Handler:
MsgBox Err.description & Chr(13) & Chr(13) & "Please note you must have Microsoft Word installed to utilize the spell check feature.", vbCritical, "Error #: " & Err.Number
Resume Done
 End Function

Put this function in a module...

To call it what I did was allow the end user to click the F7 key to do a spell check.  You can modify this to fit your needs.  I also only spell check the textbox that has the focus or is activated.

YOu can also write code to go through a loop of textbox controls checking each one..

Heres my call in the form key down event (set the keypreview property to true in the form as well)


VB Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo Err_Handler
 Dim ctl As Control
Set ctl = Me.ActiveControl
 If KeyCode = vbKeyF7 And TypeOf ctl Is TextBox Then
    Call CheckSpelling(ctl)
Else
    'do nothing
End If
 Done:
Exit Sub
 Err_Handler:
MsgBox Err.description, vbCritical, "Error #: " & Err.Number
Resume Done
End Sub

To loop through a bunch of text box controls try this:


VB Code:
Dim ctl As Control
 For each ctl in Me.Controls
  if TypeOf ctl is TextBox then
     CheckSpelling(ctl)
  end if
next ctl

Enjoy :Alien Frog:

----------


## DKasler

> Whats WordPerfect?


God damn, I feel old... And I'm only 24.

QUESTION: What would happen if Word isnt installed? Nothing? or would the system generate an error?

----------


## brendalisalowe

Hello.  I am trying to implement a spell check on my RichTextBox.  When I copy and paste your code, it doesn't like these two lines:

App.OleRequestPendingTimeout = 999999
strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)

Any ideas why?  Thanks for any help!

----------


## MartinLiss

Have you included the Reference to Word?

----------


## brendalisalowe

Yes, I have added the Word reference, but do I need to put something like this at the top of my form?

Imports Word.??????

----------


## MartinLiss

No you don't need to do that (at least not in VB6). I've attached a working sample.

----------


## brendalisalowe

I am using VB.NET.  Do you know of any postings on spell checking in .NET?

----------


## jbhan

Hi!
Does anyone have any idea how to make the spell and grammar check multilanguage? 

Thanks!

----------


## MartinLiss

Does Word do it? If so then it should be fairly easy. If not then....?

----------


## jbhan

Check this out for spell checking in VB.NET...

http://msdn.microsoft.com/office/und...dspellchkr.asp

jb.

----------


## jbhan

I tried it out and it does check in multiple languages. Ex: I typed Achtung by default it shows it as an incorrect spelling since the default language is English. If I select the word and right click , select the language as German it recognizes it as valid.

I don't know how to leverage this  programatically. How do you tell word which language it should do the spelling and grammar check in?

Thanks!
jb

----------


## MartinLiss

Record a macro while doing what you just did and then add the "commands" to my code.

----------


## jbhan

Got it! This is what I got in the macro..


VB Code:
Sub SpellCheck()
'
' SpellCheck Macro
'
    Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
    Selection.LanguageID = wdGerman
    Selection.NoProofing = False
    Application.CheckLanguage = False
End Sub


I'll try it out in the project.
Thank you! That was a great idea!

jb

----------


## asgsoft

can i use that in my html editor?

----------


## JamesNZ

> 3 When users try to use other applications while the spellchecker is in progress the screen gets confused and appears to crash.


Do you have a fix for this Martin? 

Pretty annoying for the user because if they switch to another application and switch back it's frozen and they have to ctrl/alt/delete to end the application task.

----------


## MartinLiss

Did you see post #29?

----------


## JamesNZ

Are you referring to this code?



```
    objDoc.CheckSpelling

    strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
    ' Correct the carriage returns
    strResult = Replace(strResult, Chr(13), Chr(13) & Chr(10))
    
    If Text1.Text = strResult Then
        ' There were no spelling errors, so give the user a
        ' visual signal that something happened
        MsgBox "The spelling check is complete.", vbInformation + vbOKOnly
    End If
```

----------


## JamesNZ

I don't think I'm missing anything. This is code I'm using:


VB Code:
Public Function CheckSpelling(t As textbox)
'SpellCheck Function
'taken from [url]http://www.vbforums.com/showthread.php?t=246451[/url]
On Error GoTo Err_Handler
     Dim objWord As Object
    Dim objDoc  As Object
    Dim strResult As String
    
    'Create a new instance of word Application
    App.OleRequestPendingTimeout = 999999
    Set objWord = CreateObject("word.Application")
    objWord.Visible = False
    
    Set objDoc = objWord.Documents.Add(, , 1, True)
     objDoc.Content = t.text
    objDoc.CheckSpelling
    objWord.Visible = False
    
    strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
    'correct the carriage returns
    strResult = Replace(strResult, Chr(13), Chr(13) & Chr(10))
     If t.text = strResult Then
        ' There were no spelling errors, so give the user a
        ' visual signal that something happened
        MsgBox "The spell check is complete!", vbInformation + vbOKOnly, "Spelling Complete"
    End If
     'Clean up
    objDoc.Close False
    Set objDoc = Nothing
    objWord.Application.Quit True
    Set objWord = Nothing
     ' Replace the selected text with the corrected text. It's important that
    ' this be done after the "Clean Up" because otherwise there are problems
    ' with the screen not repainting
    t.text = strResult
 Exit Function
 'in case user does not have word...
Err_Handler:
MsgBox Err.Description & Chr(13) & Chr(13) & "Please note you must have Microsoft Word installed to utilize the spell check feature.", vbCritical, "Error #: " & Err.Number
 End Function

----------


## MartinLiss

Sorry but since I don't use VBA I can't help. Maybe user rike has some insight into the problem. If you do solve it please let me know.

----------


## JamesNZ

Ok - thanks anyway Martin.

----------


## rike

Sorry I was unable to find a fix for this.

----------


## PokeyDillard

Alright, I'm using this code to check the spelling on a text box when it loses focus. The problem is that if I you want the "mispelled" word to remain and you choose "ignore", I get:

*Runtime Error '5'* 
*Invalid procedure call or argument*

If you are running it from the editor (testing) you don't get this error...if you make an executable you get the error.

Please help.

----------


## MartinLiss

I just tried to reproduce your problem and I couldn't. If you attach your project I'll take a look at it.

----------


## hdtvluvr

This code is wonderful and will meet my needs if I can get it to work. Using Word and Access 2003. The code runs well until the last line:
Comments.Text = strResult

where Comments is the name of the text box.

The message I get is:

Run-Time error '2115'
The macro or function set to the BeforeUpdate or Validation Rule property for this field is preventing the database from saving the data in the field.

There isn't any code associated with the BeforeUpdate or Validation Rule properties for the field.

OK, I kept trying and decided to use the code for a Function and change the OnKeyDown event for F7. When done this way, the error above is gone but when the text box is checked and corrected, Spell Check continues to the first record of the database and the first textbox on the form.

Just stepped through the code and it all runs. When it finishes, it seems that the regular spell check associated with F7 runs. How can I stop this from happening?

It seems I am almost there.

Thanks

----------


## MartinLiss

Welcome to VB Forums!

You might want to check this out.

----------


## hdtvluvr

Thanks for the link. I copied the code and put it in a new module. I couldn't get it to even compile. It doesn't like 
Public moApp As Word.Application
or 
Set oDoc = moApp.Documents.Add(, , 1, True)

I don't know if there are other problems.

----------


## dglienna

You have to set a reference to WORD in the project first.



> Microsoft Word x.0 Object Library


Use whatever version that you have.  I have Office XP, which is 10.0

----------


## hdtvluvr

I made the reference and there was other code that caused problems such as clipboard.clear

Thanks for your help. I'll work with the other code and see if I can stop F7 from running the normal Spell Check after the spell check in the code.

----------


## PokeyDillard

Martin,

Here is my code. I don't know if you'll be able to get it to run...it has an Oracle database under it. I hope you (or someone else) can help.

----------


## MartinLiss

Sorry, but I couldn't even come close to running it since I don't have a dll and a bunch of ocx's it's looking for. If you can tell me which form(s) have the spell checking I'll try to see if I can find anything wrong. Another way we could go about it would be if you could create a plain-vanilla app that has the problem.

----------


## PokeyDillard

Sorry for the long delay on posting a message back. The form that contains the problem is frmReview (the Description field is where I'm testing). The spell checker code is in modSpelling.

I tried to make a plain-vanilla app that had the same problem and guess what...I can't. A similiar program with the same general idea will run spell check without a problem.

----------


## MartinLiss

Try adding the highlighted code and perhaps some additional error handling if and when to get the new message.


VB Code:
Public Sub CheckSpelling(t As Control) '(t As TextBox)
On Error GoTo Err_Handler
     Dim objWord As Object
    Dim objDoc  As Object
    Dim strResult As String
        'Create a new instance of word Application
     If Len(t.Text) > 0 Then
        App.OleRequestPendingTimeout = 999999
         Set objWord = CreateObject("Word.Application")
        objWord.Visible = False
         Select Case objWord.Version
            Case "9.0", "10.0", "11.0" 'Office 2000, xp, 2k3
                Set objDoc = objWord.Documents.Add(, , 1, True)
            Case Else 'Office 97
                Set objDoc = objWord.Documents.Add
        End Select
         objDoc.Content = t.Text
        objDoc.CheckSpelling
 [HL="#FFFF80"]Dim myErr As Long
myErr = objWord.ActiveDocument.SpellingErrors.Count
'User pressed Cancel button
'(Pressing Ignore reduces the count, pressing Cancel doesn't)
If myErr > 0 Then
    MsgBox "Cancelled"
End If[/HL]
        strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
        'correct the carriage returns
        strResult = Replace(strResult, Chr(13), Chr(13) & Chr(10))
         If t.Text = strResult Then
            ' There were no spelling errors, so give the user a visual signal that something happened
            MsgBox "The spelling check is complete.", vbInformation + vbOKOnly, "Spelling Complete"
        End If
        
        'Clean up
        objDoc.Close False
        Set objDoc = Nothing
        objWord.Application.Quit True
        Set objWord = Nothing
         ' Replace the selected text with the corrected text. It's important that
        ' this be done after the "Clean Up" because otherwise there are problems
        ' with the screen not repainting
        t.Text = strResult
    End If
 Done:
Exit Sub
 'in case user does not have word...
Err_Handler:
MsgBox Err.Description & Chr(13) & Chr(13) & "Please note you must have Microsoft Word installed to utilize the spell check feature.", vbCritical, "Error #: " & Err.Number
Resume Done
 End Sub

----------


## PokeyDillard

I added the highlighted code. I changed it to show the number of ...SpellingErrors.Count in a message box b/c the ignore word didn't fire the message.

It still errors out, before it runs that highlighted code.

----------


## MartinLiss

I'm sorry you are having a problem but I can't help any futher unless I have a project that I can run that shows the problem.

----------


## PokeyDillard

I'm a genius!!!!

I figured out the problem I was encountering. I moved the clean up of the object to before the message box informing the user that the check was complete. This seems to have corrected the problem of the Run-time error. I guess it was loosing reference to the word document object.

GO ME!

----------


## mccrerr

I am able to accomplish this, but can anybody tell me how to apply this to a Rich Text Box without loosing the Rich Text Formattin.

----------


## turnertrends

I write a newsletter using vb. The spell check code you have provided is outstanding.  I am encounting a problem though.  I often have html code (for tables) embedded in my text.  When I use this spell check code, it finds a lot of duplicate or repeating 'words' that are just repeating html code.  One set in particular is picked up a lot: "0px 0px 0px 0px".  The winword code assumes the 0px is repeated and an error.  It asks me to "Ignore" or "Delete".  There can be 100's of these in my code.  Is there any way to tell winword or code that these repeating 'words' are not to be checked?

Thanks in advance!

----------


## MartinLiss

> I write a newsletter using vb. The spell check code you have provided is outstanding.  I am encounting a problem though.  I often have html code (for tables) embedded in my text.  When I use this spell check code, it finds a lot of duplicate or repeating 'words' that are just repeating html code.  One set in particular is picked up a lot: "0px 0px 0px 0px".  The winword code assumes the 0px is repeated and an error.  It asks me to "Ignore" or "Delete".  There can be 100's of these in my code.  Is there any way to tell winword or code that these repeating 'words' are not to be checked?
> 
> Thanks in advance!


I don't currently have time to look into that but the code is there for you to do what you want with, so you can give it a try if you want to.

----------


## RobDog888

> I write a newsletter using vb. The spell check code you have provided is outstanding.  I am encounting a problem though.  I often have html code (for tables) embedded in my text.  When I use this spell check code, it finds a lot of duplicate or repeating 'words' that are just repeating html code.  One set in particular is picked up a lot: "0px 0px 0px 0px".  The winword code assumes the 0px is repeated and an error.  It asks me to "Ignore" or "Delete".  There can be 100's of these in my code.  Is there any way to tell winword or code that these repeating 'words' are not to be checked?
> 
> Thanks in advance!


You can add the html code that is giving you issues to a custom dictionary or just add it to the default dictionary.

----------


## Dickson

Hi there,

May I know what is the different between   objWord.Documents.Add(, , 1, True) and  Set objDoc = objWord.Documents.Add for different version of objWord.Documents?

Thanks

Dick

----------


## RobDog888

Word 97 did not support the arguments as 200 and newer does.

----------


## Dickson

May I know what are the argurments stand for (,,1,true) and what will happen if i omit those argurments in words 2k and above?

thanks in advance

Dick

----------


## RobDog888

Its all optional arguments but these are what they are in 200+

.Documents.Add([Template], [NewTemplate], [DocumentType], [Visible])

You can specify a template to base the document creation upon, specify this is to be a new template, doc type has several types it could be, and if to show the document or make it hidden.
All these arguments can be seen in the Object Browser of Word and help file too for more info then what I posted.  :Smilie: .

----------


## purge98

Hello,

I'm new here and have just found your spell check VB code.

It works a treat as a function embedded in a Module/Form BUT the Word spell check box/form is hidden.

The only way I can get to see it is if I clear the desktop and then click on my program's task bar button. 

Do you know a way of forcing the spell check form to the front and visible?

(I am running Vista  :Mad:  and have noticed that your example in #44 still hides the spell check form as above. Not sure if this is relevant)

Thanks

----------


## MartinLiss

Sorry, but since I assume that your problem is Vista related and I don't have Vista, I can't do anything about it. You might want to try the spell checker in RobDog888's signature.

----------


## purge98

Thanks for the quick reply Mr Martin.

I have seen Mr Dog888's code but it is too bespoke/specialized for my application. I need a more integrated function like yours.

I have seen this at the MS site

http://support.microsoft.com/kb/243844/en-us

which doesn't hide the Spell checker quite as well in Vista  :Smilie:  

It is visible in the taskbar however and so it just needs one to click it to reveal it. Not too bad a problem.

I will use your error checking etc to check for the presence of Word and also produce a msgbox to tell the user when the spell check is complete.

Thank you for your help Mr Martin. I have rated you  :Thumb:

----------


## tony007

It is very good program. 

Is it possible that it do spell checking for editbox loated in *external applications * .There is an application that has richedit box with following class information. Could you tell me how to make your program work with it? For example after typing on that application i want to hit F11 and it does spell check for me. 

Furthermore, i have word xp is it possible to do spell checking as i type by getting red line under words that are not correct?

Looking forward for your reply.Thanks

RichEdit20A
#32770
AtlAxWin71
ATL:006ED050
WTL_SplitterWindow
WTL_SplitterWindow
WTL_SplitterWindow
WTL_SplitterWindow
DlgGroupChat Window Class


window that holds that edit box class infor:
*DlgGroupChat Window Class*

----------


## MartinLiss

I'm sorry but I can't help you with that.

----------


## jptim

I'd love to use this code to add the spell check function to my app.  I have users ranging from word 2K to 2K7, so does anyone know if there is a modification that is needed to include word version beyond 2k3?   Also, I will be running the code from forms in access 2k7 runtime only.  Doing a direct reference to spellcheck causes an error on any machines without office 2k7, so this code seems like the right trick?

----------


## Azeccia

Great code guys thanks alot.

----------


## RobertLees

Is this thread still being looked at ? I have a problem with the following...

strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)

There is no 'left' in vb.net I tried substring, but objDoc.Content (or.ToString) does not give the corrected text.

----------


## MartinLiss

The code as supplied is VB6 and not .Net.

----------


## RobertLees

...the rest works OK in .net. What changes to I have to make for .net, or is there a totaly different approach ?

----------


## MFL (UK)

I am new to this forum and this is my first post.  Not sure if this thread is still active but am seeking advice as a newbie!

I have applied Martin's code (its excellent!) to my application (VB6, Office 2003 under XP).  It all works fine apart from one annoying behaviour I cannot figure.  When the Word spell checker form is displayed, it does not always appear at the front of the screen.  But this behaviour is inconsistent - sometimes it does, and sometimes not and I have been unable to identify why this variation is occurring.
Is there a foolproof method of forcing the spell checker form to the front of the display area when the spell checking is invoked?

----------


## MartinLiss

As far as I remember nobody else has reported this issue, but I'll see if I can reproduce it. In any case you might want to try RobDog's similar code here.

----------


## MFL (UK)

Martin,

Thanks for the prompt response - I may be being dense (sorry!) but the link for Robdog's code doesn't appear to take me anywhere!  Can you advise where I can see his code to try it?   :Embarrassment:  

Thanks

----------


## MartinLiss

Try clicking the *VB/Office Guru Word SpellChecker VB6* link in his signature (see post #81).

----------


## cworkman29729

Anyone Know How To Do This In C#?

----------


## RobDog888

I have a VB.NET version in my signature and you can use one of those converters to convert it to C#

----------


## wiz....

can it be done for Office 2007??????////////

sorry fr reviving this old post!!!!!!

thanx in advance
wiz....

----------


## MartinLiss

Did you read the last half-dozen or so posts?

----------


## wiz....

i have been and cudn't find any thing related to Office 2007!!!!

----------


## RobDog888

You only need to use Late Binding in order to support multiple compatible versions. See my FAQ on Late Binding - http://www.vbforums.com/showthread.php?t=406640 for more and code examples.

----------

