1. Home
  2. Computing & Technology
  3. Visual Basic

VBA - The Word Working Partner - Part 3

By Dan Mabbutt, About.com

7 of 7

Automate Find and Replace Example - Page 5

The other recorded properties are also left out for simplicity - except for Selection.Find.Wrap = wdFindContinue which really is necessary. For a production macro, you might want to add them back in just to be on the safe side.

~~~~~~~~~~~~~~~~~~~~~~~~~
Sub ReplaceText()
Dim FindStringArray(1000) As String
Dim ReplaceStringArray(1000) As String
Dim i As Integer
Dim ArrayEnd As Integer
' Initialize the arrays
' with the find and replace strings
FindStringArray(0) = "find 0"
ReplaceStringArray(0) = "replace 0"
FindStringArray(1) = "find 1"
ReplaceStringArray(1) = "replace 1"
' The last element used in the arrays
ArrayEnd = 1
' Necessary to start at the beginning
' for each Find
Selection.Find.Wrap = wdFindContinue
For i = 0 To ArrayEnd
  With Selection.Find
    .Text = FindStringArray(i)
    .Replacement.Text = ReplaceStringArray(i)
    .Execute Replace:=wdReplaceAll
  End With
Next 'i
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~
download the document and macro

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB 6
  5. Learn VBA
  6. VBA - The Word Working Partner - Part 3

©2009 About.com, a part of The New York Times Company.

All rights reserved.