Page 1 of 3 123 LastLast
Results 1 to 40 of 101

Thread: VB - Spell Check a Textbox

Hybrid View

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    VB - Spell Check a Textbox

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

    VB Code:
    1. Dim objWord As Object
    2.     Dim objDoc  As Object
    3.     Dim strResult As String
    4.    
    5.     'Create a new instance of word Application
    6.     Set objWord = CreateObject("word.Application")
    7.  
    8.     Select Case objWord.Version
    9.         'Office 2000
    10.         Case "9.0"
    11.             Set objDoc = objWord.Documents.Add(, , 1, True)
    12.         'Office XP
    13.         Case "10.0"
    14.             Set objDoc = objWord.Documents.Add(, , 1, True)
    15.         'Office 97
    16.         Case Else ' Office 97
    17.             Set objDoc = objWord.Documents.Add
    18.     End Select
    19.  
    20.     objDoc.Content = Text1.Text
    21.     objDoc.CheckSpelling
    22.  
    23.     strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
    24.  
    25.     If Text1.Text = strResult Then
    26.         ' There were no spelling errors, so give the user a
    27.         ' visual signal that something happened
    28.         MsgBox "The spelling check is complete.", vbInformation + vbOKOnly
    29.     End If
    30.    
    31.     'Clean up
    32.     objDoc.Close False
    33.     Set objDoc = Nothing
    34.     objWord.Application.Quit True
    35.     Set objWord = Nothing
    36.  
    37.     ' Replace the selected text with the corrected text. It's important that
    38.     ' this be done after the "Clean Up" because otherwise there are problems
    39.     ' with the screen not repainting
    40.     Text1.Text = strResult
    41.  
    42.     Exit Sub

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'm guessing Word has to be installed for this to work?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Yup
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Bugger
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Lively Member
    Join Date
    Jun 2001
    Posts
    107
    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

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. strResult = objDoc.Content.Substring(0,objDoc.Content.Length-1)

    Probably something like that.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    New Member Tormentlord's Avatar
    Join Date
    Sep 2003
    Location
    Charon
    Posts
    4

    wow helpful

    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?
    "I grow tired of you inscrupulant drivel, foolish mortal."-Tormentlord

    tormentlord@yahoo.com
    achilles_ian@hotmail.com

    www.darkism.8m.com

  8. #8

  9. #9
    New Member
    Join Date
    Sep 2001
    Posts
    9

    component request pending

    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.

  10. #10

  11. #11
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Thumbs up Spell and Grammar Checker

    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:
    1. objDoc.CheckSpelling

    to

    VB Code:
    1. 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?
    Last edited by Mark Gambo; Dec 18th, 2003 at 03:31 PM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  12. #12

  13. #13
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Whats WordPerfect?

  14. #14
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: VB - Spell Check a Textbox

    Quote Originally Posted by Madboy
    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?
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  15. #15
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    Re: VB - Spell Check a Textbox

    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!
    Brenda

    If it weren't for you guys, where would I be?

  16. #16

  17. #17
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    Re: VB - Spell Check a Textbox

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

    Imports Word.??????
    Brenda

    If it weren't for you guys, where would I be?

  18. #18
    New Member
    Join Date
    Feb 2005
    Posts
    4

    Question Re: VB - Spell Check a Textbox

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

    Thanks!

  19. #19

  20. #20
    New Member
    Join Date
    Feb 2005
    Posts
    4

    Re: VB - Spell Check a Textbox

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

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

    jb.

  21. #21
    New Member
    Join Date
    Feb 2005
    Posts
    4

    Re: VB - Spell Check a Textbox

    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

  22. #22
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Wellington, NZ
    Posts
    267

    Re: VB - Spell Check a Textbox

    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.

  23. #23

  24. #24
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Wellington, NZ
    Posts
    267

    Re: VB - Spell Check a Textbox

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

  25. #25

  26. #26
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Sounds good, does anybody use it?

  27. #27

  28. #28
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    hehehe, i didnt realise. Im only 17 so there you go, didnt realise such a thing existed.

    Well, you learn something new everyday

  29. #29
    Member
    Join Date
    Nov 2002
    Posts
    53
    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

  30. #30

  31. #31
    Member
    Join Date
    Nov 2002
    Posts
    53
    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:
    1. Private Sub Command0_Click()
    2.     Dim objWord As Object
    3.     Dim objDoc1, ojbDoc2  As Object
    4.     Dim strResult1, strResult2 As String
    5.    
    6.     'Create a new instance of word Application
    7.     Set objWord = CreateObject("word.Application")
    8.  
    9.     Select Case objWord.Version
    10.         'Office 2000
    11.         Case "9.0"
    12.             Set objDoc = objWord.Documents.Add(, , 1, True)
    13.  
    14.         'Office XP
    15.         Case "10.0", "11.0"
    16.             Set objDoc1 = objWord.Documents.Add(, , 1, True)
    17.             Set objDoc2 = objWord.Documents.Add(, , 1, True)
    18.                    
    19.         'Office 97
    20.         Case Else ' Office 97
    21.             Set objDoc = objWord.Documents.Add
    22.     End Select
    23.  
    24.     Me.Text1.SetFocus
    25.     objDoc1.Content = Text1.Text
    26.     objDoc1.CheckSpelling
    27.  
    28.     strResult1 = Left(objDoc1.Content, Len(objDoc1.Content) - 1)
    29.    
    30.    
    31.    
    32.     Me.Text2.SetFocus
    33.     objDoc2.Content = Text2.Text
    34.     objDoc2.CheckSpelling
    35.  
    36.  
    37.     strResult2 = Left(objDoc2.Content, Len(objDoc2.Content) - 1)
    38.  
    39.     If Text2.Text = strResult2 Then
    40.         Text1.SetFocus
    41.         If Text1.Text = strResult1 Then
    42.             ' There were no spelling errors, so give the user a
    43.             ' visual signal that something happened
    44.             MsgBox "The spelling check is complete.", vbInformation + vbOKOnly
    45.         End If
    46.     End If
    47.    
    48.     'Clean up
    49.     objDoc1.Close False
    50.     objDoc2.Close False
    51.     Set objDoc1 = Nothing
    52.     Set objDoc2 = Nothing
    53.     objWord.Application.Quit True
    54.     Set objWord = Nothing
    55.  
    56.     ' Replace the selected text with the corrected text. It's important that
    57.     ' this be done after the "Clean Up" because otherwise there are problems
    58.     ' with the screen not repainting
    59.     Text1.SetFocus
    60.     Text1.Text = strResult1
    61.     Text2.SetFocus
    62.     Text2.Text = strResult2
    63.  
    64.     Exit Sub
    65. End Sub

    How would you approach this?

  32. #32

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427
    VB Code:
    1. Private Sub Command0_Click()
    2.     Dim objWord As Object
    3.     Dim objDoc  As Object
    4.     Dim strResult As String
    5.     Dim ctl As Control
    6.    
    7.     App.OleRequestPendingTimeout = 999999
    8.     'Create a new instance of word Application
    9.     Set objWord = CreateObject("word.Application")
    10.  
    11.     Select Case objWord.Version
    12.         'Office 2000
    13.         Case "9.0"
    14.             Set objDoc = objWord.Documents.Add(, , 1, True)
    15.         'Office XP
    16.         Case "10.0"
    17.             Set objDoc = objWord.Documents.Add(, , 1, True)
    18.         'Office 97
    19.         Case Else ' Office 97
    20.             Set objDoc = objWord.Documents.Add
    21.     End Select
    22.    
    23.     For Each ctl In Controls
    24.         If TypeOf ctl Is TextBox Then
    25.             objDoc.Content = ctl.Text
    26.             objDoc.CheckSpelling
    27.        
    28.             strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
    29.        
    30.             If ctl.Text = strResult Then
    31.                 ' There were no spelling errors, so give the user a
    32.                 ' visual signal that something happened
    33.                 MsgBox "The spelling checking for " & ctl.Name & " is complete.", vbInformation + vbOKOnly
    34.             End If
    35.            
    36.         End If
    37.     Next
    38.     'Clean up
    39.     objDoc.Close False
    40.     Set objDoc = Nothing
    41.     objWord.Application.Quit True
    42.     Set objWord = Nothing
    43.  
    44.     ' Replace the selected text with the corrected text. It's important that
    45.     ' this be done after the "Clean Up" because otherwise there are problems
    46.     ' with the screen not repainting
    47.     Text1.Text = strResult
    48.  
    49. 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.

  33. #33
    Member
    Join Date
    Nov 2002
    Posts
    53
    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:
    1. Private Sub Command0_Click()
    2.     Dim objWord As Object
    3.     Dim objDoc  As Object
    4.     Dim strResult As String
    5.     Dim ctl As Control
    6.    
    7.    
    8.     'Create a new instance of word Application
    9.     Set objWord = CreateObject("word.Application")
    10.  
    11.     Select Case objWord.Version
    12.         'Office 2000
    13.         Case "9.0"
    14.             Set objDoc = objWord.Documents.Add(, , 1, True)
    15.         'Office XP
    16.         Case "10.0", "11.0"
    17.             Set objDoc = objWord.Documents.Add(, , 1, True)
    18.         'Office 97
    19.         Case Else ' Office 97
    20.             Set objDoc = objWord.Documents.Add
    21.     End Select
    22.    
    23.     For Each ctl In Controls
    24.         If TypeOf ctl Is TextBox Then
    25.             ctl.SetFocus
    26.             objDoc.Content = ctl.Text
    27.             objDoc.CheckSpelling
    28.        
    29.             strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
    30.        
    31.             If ctl.Text = strResult Then
    32.                 ' There were no spelling errors, so give the user a
    33.                 ' visual signal that something happened
    34.                 MsgBox "The spelling checking for " & ctl.Name & " is complete.", vbInformation + vbOKOnly
    35.             End If
    36.            
    37.         End If
    38.     Next
    39.     'Clean up
    40.     objDoc.Close False
    41.     Set objDoc = Nothing
    42.     objWord.Application.Quit True
    43.     Set objWord = Nothing
    44.  
    45.     ' Replace the selected text with the corrected text. It's important that
    46.     ' this be done after the "Clean Up" because otherwise there are problems
    47.     ' with the screen not repainting
    48.     Text1.SetFocus
    49.     Text1.Text = strResult
    50.  
    51. End Sub

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

  34. #34

  35. #35
    Member
    Join Date
    Nov 2002
    Posts
    53
    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.

  36. #36

  37. #37
    Member
    Join Date
    Nov 2002
    Posts
    53
    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.

  38. #38

  39. #39

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427
    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:
    1. objDoc.CheckSpelling
    2.  
    3.     strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
    4.     ' Correct the carriage returns
    5.     [b]strResult = Replace(strResult, Chr(13), Chr(13) & Chr(10))[/b]
    6.    
    7.     If Text1.Text = strResult Then
    8.         ' There were no spelling errors, so give the user a
    9.         ' visual signal that something happened
    10.         MsgBox "The spelling check is complete.", vbInformation + vbOKOnly
    11.     End If

  40. #40
    Lively Member
    Join Date
    Mar 2003
    Posts
    126

    Re: VB - Spell Check a Textbox

    Quote Originally Posted by MartinLiss
    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.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width