-
May 23rd, 2003, 11:30 AM
#1
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
-
Jun 11th, 2003, 11:41 AM
#2
Stuck in the 80s
I'm guessing Word has to be installed for this to work?
-
Jun 11th, 2003, 12:04 PM
#3
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 11th, 2003, 02:00 PM
#4
-
Sep 26th, 2003, 11:48 AM
#5
Lively Member
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
-
Sep 26th, 2003, 02:56 PM
#6
VB Code:
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
-
Sep 29th, 2003, 09:07 AM
#7
New Member
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
-
Sep 29th, 2003, 10:02 AM
#8
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.
-
Oct 14th, 2003, 12:24 AM
#9
New Member
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.
-
Oct 14th, 2003, 11:05 AM
#10
Add the following before the word object is created.
App.OleRequestPendingTimeout = 999999
-
Dec 17th, 2003, 08:43 PM
#11
-
Dec 17th, 2003, 09:11 PM
#12
If Wordperfect has an object module that exposes the right properties then I guess so, but I've no experience with it.
-
Dec 19th, 2003, 04:17 PM
#13
Supreme User
-
Dec 19th, 2003, 04:37 PM
#14
-
Dec 19th, 2003, 04:40 PM
#15
Supreme User
Sounds good, does anybody use it?
-
Dec 19th, 2003, 04:58 PM
#16
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.
-
Dec 19th, 2003, 05:03 PM
#17
Supreme User
hehehe, i didnt realise. Im only 17 so there you go, didnt realise such a thing existed.
Well, you learn something new everyday
-
Jan 4th, 2004, 08:45 PM
#18
Member
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
-
Jan 4th, 2004, 10:45 PM
#19
Please post your code; at least the cmdSpell_Click() sub.
-
Jan 5th, 2004, 04:28 PM
#20
Member
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?
-
Jan 5th, 2004, 05:51 PM
#21
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.
-
Jan 5th, 2004, 06:59 PM
#22
Member
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' ?
-
Jan 5th, 2004, 09:05 PM
#23
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.
-
Jan 5th, 2004, 10:47 PM
#24
Member
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.
-
Jan 6th, 2004, 12:33 PM
#25
I may not be able to do anything about it but would you like to send me your project?
-
Jan 6th, 2004, 03:21 PM
#26
Member
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.
-
Jan 6th, 2004, 03:24 PM
#27
How about attaching that project or creating a new one that demonstrates the problem.
-
Jan 15th, 2004, 11:44 AM
#28
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
-
Mar 8th, 2004, 10:17 PM
#29
Frenzied Member
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?
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Mar 9th, 2004, 08:28 AM
#30
Banned
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
-
Mar 9th, 2004, 08:58 AM
#31
Frenzied Member
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..
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Mar 9th, 2004, 09:03 AM
#32
Banned
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.
-
Mar 9th, 2004, 09:05 AM
#33
Frenzied Member
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.
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Mar 9th, 2004, 10:39 AM
#34
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.
-
Mar 9th, 2004, 11:11 AM
#35
Frenzied Member
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..
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Jul 2nd, 2004, 10:30 AM
#36
Banned
Re: VB - Spell Check a Textbox
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 ?
-
Jul 2nd, 2004, 10:54 AM
#37
Re: Re: VB - Spell Check a Textbox
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.
-
Jul 2nd, 2004, 02:11 PM
#38
Banned
Re: Re: Re: VB - Spell Check a Textbox
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
-
Jul 2nd, 2004, 03:03 PM
#39
Banned
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
-
Jan 17th, 2005, 01:17 PM
#40
Addicted Member
Re: VB - Spell Check a Textbox
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|