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
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.
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 :)
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?