View Single Post
 
Old 06-12-2018, 04:10 PM
rjmiller rjmiller is offline Windows 10 Office 2013
Novice
 
Join Date: Jun 2018
Posts: 4
rjmiller is on a distinguished road
Default Spell Check TextBox in UserForm

I've been searching other forums and posts looking for a suitable vba example to spell check text within a userform textbox. I located a post from 2003 that had the most effective solution (I had to make one modification to get it to work):

Code:
Private Sub cmdSpellCheck_Click()
    'Spell check certain fields in a UserForm
    'Adapted from http://computer-programming-forum.com/1-vba/3853759c12e8d3ae.htm
    'By Ann K Bohma, Bra Utbildning AB, Sweden

    'Create a new document
    Dim oDoc2 As Document
    Set oDoc2 = Documents.Add
    With oDoc2
        'Set language and style (can be omitted)
        '.Styles(wdStyleNormal).LanguageID = wdEnglish
        '.Range.Style = wdStyleNormal
        DoSpellCheck TextBox1
        'etc etc or Loop all controls checking for textcontrols
        
        .SpellingChecked = True 'can be omitted,
        .Close wdDoNotSaveChanges
    End With

    MsgBox "Spelling and Grammar Check Complete"

End Sub

Private Sub DoSpellCheck(oTxtBox As Control)

    Dim oRng As Range
    Dim strText As String
    With oTxtBox
        If Not .Value = "" And Not .Value = "<WhatEver>" Then
            Set oRng = oDoc2.Range
            oRng = .Value
            With oRng
                .CheckSpelling AlwaysSuggest:=True
                .CheckGrammar 'I added grammar to the code from the post
                'Set the range eliminating the last CR/Enter
                .SetRange .Start, .End - 1
            End With
            'Return data to the Userform
            .Value = oRng.Text
        End If
    End With
End Sub

Last edited by rjmiller; 06-13-2018 at 01:45 PM. Reason: Changed "Set oRng = ActiveDocument.Range" to "oDoc2.Range" - as suggested by Guessed
Reply With Quote