WD97: How to Use AutoCorrect on an Existing Document (262220)



The information in this article applies to:

  • Microsoft Word 97 for Windows

This article was previously published under Q262220

SUMMARY

The AutoCorrect feature works dynamically when you type a space after a word that appears in the AutoCorrect list. This feature operates while you create the document, but Word does not automatically scan a previously-created document and make AutoCorrect changes.

This article provides a sample macro that compares each word in a Word document to words in the AutoCorrect list and then replaces the matched words with the text string specified in the AutoCorrect list.

NOTE: In some cases, this macro may take a long time to run. To stop the macro before it is finished, press CTRL+BREAK and then click End when you receive the following Microsoft Visual Basic message:
Code execution has been interrupted
When you save the document, Word saves all changes you made up until you pressed CTRL+BREAK.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

173707 OFF97: How to Run Sample Code from Knowledge Base Articles

Place the following code in the Declarations section of your code page:
Option Explicit

Dim anEntry
Dim myMatch
Dim myConfirm
				
Create the following macro to search your active document, identify words that appear in both the document and the AutoCorrect list, prompt you to replace the word, and then insert the corresponding AutoCorrect text.
Sub ReplaceAutoCorrectText()

Application.ScreenUpdating = True
Selection.HomeKey unit:=wdStory, Extend:=wdMove
   
With ActiveDocument.Content.Find
   .ClearFormatting
   
   Do While .Execute(FindText:=" ", Forward:=True, Format:=True) = True
   
      Selection.MoveRight unit:=wdWord, Count:=1, Extend:=wdExtend
        
      For Each anEntry In AutoCorrect.Entries
         If anEntry.Name = Selection Then
            Selection.Words(1).Select
            myMatch = True
            myConfirm = MsgBox("Are you sure you want to replace " _
               & Chr$(34) & anEntry.Name & Chr$(34) & " with " & Chr$(34) _
               & anEntry.Value & Chr$(34) & "?", vbYesNo)
                  If myConfirm = vbYes Then
                     Selection.TypeText (anEntry.Value)
                     Exit For
                  End If
         End If
      Next anEntry

      Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
        
      For Each anEntry In AutoCorrect.Entries
         If anEntry.Name = Selection Then
            Selection.Words(1).Select
            myMatch = True
            myConfirm = MsgBox("Are you sure you want to replace " _
               & Chr$(34) & anEntry.Name & Chr$(34) & " with " & Chr$(34) _
               & anEntry.Value & Chr$(34) & "?", vbYesNo)
                  If myConfirm = vbYes Then
                     Selection.TypeText (anEntry.Value) & " "
                     Exit For
                  End If
         End If
      Next anEntry
        
      Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdMove
       
   Loop
End With
End Sub
				

REFERENCES

For additional information about the AutoCorrect feature in Word, click the article numbers below to view the articles in the Microsoft Knowledge Base:

159941 WD97: General Information About "Auto" Features in Word 97

186237 WD97: How to Move AutoCorrect Entries Between Computers

176751 WD97: How to Print a List of AutoCorrect Entries

180165 WD97: AutoCorrect Feature Limitations

For additional information about programming resources for Microsoft Word, click the article numbers below to view the articles in the Microsoft Knowledge Base:

163425 WD97: Macro Programming Resources

163435 VBA: Programming Resources for Visual Basic for Applications


Modification Type:MajorLast Reviewed:6/17/2005
Keywords:kbdtacode kbhowto kbinfo KB262220