How to spell check text elements in ArcMap


This sample shows how to automate Microsoft Word to do spell checking for ArcMap.

How to use

  1. Add some text to an ArcMap document.
  2. Copy and paste the code into the ArcMap VBA Editor.
  3. Add reference of Microsoft Word Object Library through VBA Editor menu: Tools->References....
  4. Switch to ArcMap and select the text element to check. Run the SpellCheck macro. The text element will be examined by Microsoft Word's spellchecker. If the spelling or grammar has an error, a dialog box will prompt for changes.
[VBA]
Public Sub SpellCheck()
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    Dim pGC As IGraphicsContainerSelect
    If pDoc.ActiveView Is pDoc.PageLayout Then
        Set pGC = pDoc.PageLayout
    Else
        Set pGC = pDoc.FocusMap
    End If
    Dim pTE As ITextElement
    'Hard coded, check the first selected element
    Set pTE = pGC.SelectedElement(0)
    
    Dim sz As String
    sz = pTE.Text
    Dim wdApp As Word.Application
    Dim wdDoc As Document
    
    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Add
    wdApp.Selection.Text = sz
    wdApp.Dialogs(wdDialogToolsSpellingAndGrammar).Show
    ' if the Cancel button is clicked, there will be one character
    If Len(wdApp.Selection.Text) > 1 Then
        pTE.Text = wdApp.Selection.Text
    Else
        wdApp.Quit
        Set wdApp = Nothing
        Exit Sub
    End If
    wdDoc.Close wdDoNotSaveChanges
    wdApp.Quit
    Set wdApp = Nothing
    pDoc.ActiveView.Refresh
End Sub






Additional Requirements
  • Microsoft Word object library