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