Spell Check UserForm Text Entries

Home Up Odds & Ends Photo Gallery Search Contact Me

 

 

The information in this website is provided without risk or obligation and free of charge.  However, if you have benefitted from my efforts here and would like to make a contribution to help me continue and maintain this work then any donation will be greatly appreciated. Please click the adjacent button to access PayPal.  Thank you.
 

This Microsoft Word Help and Tips Page provides you a method to spell check UserForm textbox entries.  There is no built-in method to check for spelling errors contained in a UserForm textbox.  Whatever the user enters is processed "as is."  If textbox text is transferred to the document with errors, those errors will need to be fixed in the document.  In protected documents this presents problems of its own.

Here is a fanciful example of a textbox with plenty of unsightly errors.

Clicking on the "Check Spelling" command button will launch the familiar "Check Spelling" dialog box.

After you have processed each of the identified spelling errors, the corrected text is displayed in the UserForm Textbox.

And clicking the "Finished" command button transfers the corrected text to the document.

The process of spell checking a UserForm Textbox includes creating a temporary document (a scratchpad).  Code contained in a single Command Button in the Userform transfers the Textbox text to the scracthpad where the spelling check dialog is invoked.  Once the errors are corrected, the text is transferred back and displayed in the Textbox.  The code used is shown below:

 
Private Sub cmdCheck_Click()
Dim oScratchPad As Word.Document
Dim oCtr As Control
Dim oRng As Word.Range
Dim bChecked As Boolean
'Open a new document to serve as a scratchpad
If oScratchPad Is Nothing Then
   Set oScratchPad = Documents.Add(Visible:=False)
  oScratchPad.Windows.Add
'**** See note below
  oScratchPad.Windows(2).Visible = False
'****
Else
  oScratchPad.Range.Delete
End If
Set oScratchPad = Documents.Add
'Iterate through all UserForm Controls
For Each oCtr In Me.Controls
    'Process TextBox Controls
    If TypeOf oCtr Is MSForms.TextBox Then
        With oCtr
            If .Value = "" Then
                bChecked = True
            Else
                Set oRng = oScratchPad.Range
                'Write the TextBox content to the scratchpad
              
 oRng = .Value
                'Check and correct spelling errors
                With oRng
                    With Dialogs(wdDialogToolsSpellingAndGrammar)
                        'If user cancels
                        If .Show = 0 Then
                            bChecked = False
                            Exit For
                        End If
                   End With
                   'Clip the end of document marker
                   .End = .End - 1
               End With
               bChecked = True
               'Write the corrected text to the TextBox
               .Value = oRng.Text
            End If
        End With
    End If
Next oCtr
Set oCtr = Nothing
oScratchPad.Close wdDoNotSaveChanges
Set oScratchPad = Nothing
If bChecked Then
    MsgBox "Check complete"
Else
    MsgBox "Spell checking was stopped in process."
End If
End Sub

****There is an apparent bug in pre-Word2007 versions that requires these extra lines of code.
 

For help with spell checking in Word Online or (protected) forms see the MVP Word FAQ "How to enable spell checker in a protected document." 

Looking for something else?

Google