-
VB - Spell Check a Textbox
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
-
I'm guessing Word has to be installed for this to work?
-
-
-
Quote:
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
-
VB Code:
strResult = objDoc.Content.Substring(0,objDoc.Content.Length-1)
Probably something like that.
-
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?
-
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.
-
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.
-
Add the following before the word object is created.
App.OleRequestPendingTimeout = 999999
-
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:
to
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: :D :)
-
If Wordperfect has an object module that exposes the right properties then I guess so, but I've no experience with it.
-
-
Quote:
Originally posted by Madboy
Whats WordPerfect?
http://www.corel.com/servlet/Satelli...=1042153063297
-
Sounds good, does anybody use it?
-
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.
-
hehehe, i didnt realise. Im only 17 so there you go, didnt realise such a thing existed.
Well, you learn something new everyday:p
-
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
-
Please post your code; at least the cmdSpell_Click() sub.
-
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?
-
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.
-
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' ?
-
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.
-
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.
-
I may not be able to do anything about it but would you like to send me your project?
-
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.
-
How about attaching that project or creating a new one that demonstrates the problem.
-
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
-
Quote:
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
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).
</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
[/B]
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?
-
Quote:
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
-
Quote:
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..
-
Quote:
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.
-
Quote:
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.
-
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.
-
Quote:
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.. :D
-
Re: VB - Spell Check a Textbox
Quote:
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
'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
Why dont you make this a function and pass a text box control?
What's the latest and greatest code ?
-
Re: Re: VB - Spell Check a Textbox
Quote:
Originally posted by jhermiz
Why dont you make this a function and pass a text box control?
Be my guest.
Quote:
Originally posted by jhermiz
What's the latest and greatest code ?
Don't know about "greatest" but that's the latest.
-
Re: Re: Re: VB - Spell Check a Textbox
Quote:
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
-
Re: Re: Re: Re: VB - Spell Check a Textbox
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:afrog:
-
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?
-
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!
-
Re: VB - Spell Check a Textbox
Have you included the Reference to Word?
-
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.??????
-
1 Attachment(s)
Re: VB - Spell Check a Textbox
No you don't need to do that (at least not in VB6). I've attached a working sample.
-
Re: VB - Spell Check a Textbox
I am using VB.NET. Do you know of any postings on spell checking in .NET?
-
Re: VB - Spell Check a Textbox
Hi!
Does anyone have any idea how to make the spell and grammar check multilanguage?
Thanks!
-
Re: VB - Spell Check a Textbox
Does Word do it? If so then it should be fairly easy. If not then....?
-
Re: VB - Spell Check a Textbox
-
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
-
Re: VB - Spell Check a Textbox
Record a macro while doing what you just did and then add the "commands" to my code.
-
Re: VB - Spell Check a Textbox
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
-
Re: VB - Spell Check a Textbox
can i use that in my html editor?
-
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.
-
Re: VB - Spell Check a Textbox
-
Re: VB - Spell Check a Textbox
Are you referring to this code?
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
-
Re: VB - Spell Check a Textbox
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
-
Re: VB - Spell Check a Textbox
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.
-
Re: VB - Spell Check a Textbox
Ok - thanks anyway Martin.
-
Re: VB - Spell Check a Textbox
Sorry I was unable to find a fix for this.
-
Re: VB - Spell Check a Textbox
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.