This sample shows how to automate Microsoft Word to do spell checking for ArcMap.
How to use
- Add some text to an ArcMap document.
- Copy and paste the code into the ArcMap VBA Editor.
- Add reference of Microsoft Word Object Library through VBA Editor menu: Tools->References....
- 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.
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