Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Spell check a VBA or VB6 ActiveX textbox

Published on
17,832 Points
5 Endorsements
Last Modified:
Martin Liss
Over 40 years of programming experience. Expand my "Full Biography" to see links to some articles I've written.
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that follows allows you to easily do just that.

A few things to note about the code.

o Microsoft Word must be installed on your PC and on any of your users' PCs in order for this code to work
o No Reference to Word is required in your application
o The code as shown will check spelling but you can also have it check grammar by changing the objDoc.CheckSpelling line  to objDoc.CheckGrammar
o You can replace the textbox with a RichTextbox if you like and the code will work with it as well
o In Excel you will need to remove the App.OleRequestPendingTimeout = 999999 line
o The code expects an ActiveX textbox named "Text1" and an ActiveX command button named "SpellCheck"

Private Sub SpellCheck_Click()

    Dim objWord As Object
    Dim objDoc  As Object
    Dim strResult As String
    Const QUOTE = """"
    On Error GoTo ErrorRoutine

    App.OleRequestPendingTimeout = 999999
    Set objWord = GetObject("Word.Application")
    If TypeName(objWord) <> "Nothing" Then
        ' Word is already open
        Set objWord = GetObject(, "Word.Application")
        ' Create an instance of Word
        Set objWord = CreateObject("Word.Application")
    End If

    Select Case objWord.version
        'Office 2000 and later
        Case "9.0", "10.0", "11.0", "14.0", "15.0"
            Set objDoc = objWord.Documents.Add(, , 1, True)
        'Office 97
        Case "8.0"
            Set objDoc = objWord.Documents.Add
        Case Else
            MsgBox "Sorry but your version of Word seems to be " & QUOTE & objWord.version _
                   & QUOTE & " and that version is not currently supported.", vbOKOnly + vbExclamation, "Spelling Checker"
            Exit Sub
    End Select

    objDoc.Content = Text1.Text
    objWord.Visible = False

    strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
    ' Reformat carriage returns
    strResult = Replace(strResult, Chr(13), Chr(13) & Chr(10))
    If Text1.Text = strResult Then
        ' There were no errors, so give the user a
        ' visual signal that something happened
        MsgBox "No changes made", vbInformation + vbOKOnly, "Spelling Checker"
    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


    Select Case Err.Number
        Case -2147221020
            ' There's no instance of Word so continue processing in order to create one
            Resume Next
        Case 429
            MsgBox "Word must be installed in order for this code to work", vbCritical + vbOKOnly, "Spelling Checker"
    End Select

End Sub

Open in new window

The following is a picture of the code in action. (Talking about 'action', the desktop picture behind the spell checker is me being tossed out of a boat while white water river rafting:) )

When the code is run, misspelled words are colored red and so after I clicked the 'Spell Check' button the word "nicly" was highlighetd in be red by Word.

If you have any comments, questions or problems please let me know.
Finally, if you find that this article has been helpful, please click the “thumb’s up” button below. Doing so lets me know what is valuable for EE members and provides direction for future articles. It also provides me with positive feedback in the form of a few points. Thanks!
Author:Martin Liss
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Join & Write a Comment

Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month