Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Spelling Check in Userform Text Fields
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!


This Microsoft Word Tips & Microsoft Word Help page provides you a method to spell check Userform textbox entries. Since 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.

spell check userform 1

spell check userform 2

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

spell check userform 3

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

spell check userform 4

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

spell check userform 5

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 temporary document and the spelling dialog is invoked. Once the errors are corrected, the text is transferred back and displayed in the Textbox. The code used is shown below:

VBA Script:
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
    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
lbl_Exit:
  Exit Sub
End Sub 

Site Note icon See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.

That's it! I hope you have found this tips page useful and informative.  You can download the demonstration documents used to create this tips page:  Spell Check Userform Text

Share

PAYMENTS/DONATIONS

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

Do you want to make a payment for consulting work or donate to help support this site?

PayPal is a safe, easy way to pay online.

Use the appropriate currency "Donate" button to make a payment or donation.


Search my site or the web using Google Search Engine

Google Search Logo