English

How To: Use VBA to check spelling in text elements or graphics

Summary

Instructions provided explain how to check the spelling of the selected text elements in a map document programmatically using Visual Basic for Applications (VBA) code.

Note:
The code sample requires Microsoft Word to be installed on the computer being used.

Procedure

Follow the steps below:

  1. In ArcMap, create a new UIButtonControl.

    A. Select Tools > Customize to open the Customize dialog box.
    B. Select the Commands tab.
    C. Select [UIControls] from the Categories list box.
    D. Select Untitled from the Save In drop-down list to save the button to this map document. Select Normal to save the button to all ArcMap documents on the machine.
    E. Click New UIControl.
    F. Select UIButtonControl and click Create.
    G. Drag the new UIButtonControl to the toolbar of choice.
    H. Close the Customize dialog box.



    Note:
    If there is already an existing UIButtonControl or the name of the UIButtonControl needs to be changed, the change needs to be made to the UIButtonControl code portion below, accordingly, before the button can be used.

  2. Open Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. Go to Tools > References...
  4. Make sure the 'Microsoft Word X.X Object Library' is checked.
  5. Click OK.
  6. In the Project Explorer window, expand the 'Normal (Normal.mxt)' item or 'Project' item and select ArcMap Objects > ThisDocument. Right-click and select View Code.
  7. Paste the following code into the code module.

    Code:
    Private Sub UIButtonControl1_Click()

    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
    Dim i As Integer
    i = 0
    Dim sz As String
    Dim wdApp As Word.Application
    Dim wdDoc As Document

    'go through each selected element
    Do Until i = pGC.ElementSelectionCount

    'skip any non text elements
    If Not TypeOf pGC.SelectedElement(i) Is ITextElement Then
    i = i + 1

    'check only text elements
    Else
    Set pTE = pGC.SelectedElement(i)
    sz = pTE.Text

    'add text to word document and open spell check
    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Add
    wdApp.Selection.Text = sz
    wdApp.Dialogs(wdDialogToolsSpellingAndGrammar).Show

    ' if Cancel button is clicked, there will be one character
    If Len(wdApp.Selection.Text) > 1 Then
    'make spelling changes to the text element
    pTE.Text = wdApp.Selection.Text
    Else
    wdApp.Quit
    Set wdApp = Nothing
    Exit Sub
    End If

    'Close Word
    wdDoc.Close wdDoNotSaveChanges
    wdApp.Quit

    'next text element
    i = i + 1
    End If
    Loop

    Set wdApp = Nothing
    pDoc.ActiveView.Refresh

    End Sub

  8. Click the new button to check the spelling of all the text elements in the map document.