E
Edward Mendelson
I am trying to write a macro that will find approximately a hundred
different strings in a document and replace them with other strings (replace
foo with bar; replace with sun with moon; replace Mars with Venus, etc.,
etc.). I've found two examples of how to do this in earlier postings (shown
below), but I wonder if anyone can suggest an efficient way of setting out
the array so that I can simply list the find and replace strings next to
each other, without providing a count of the exact number (because I will
change the number of strings as I find new ones to use), in other words
something like this:
find:=foo repl:=bar
find:=sun repl:=moon
find:=Mars repl:=Venus
etc etc
Can anyone suggest the right way to handle this? Many thanks for any help.
Edward Mendelson
(sample code below
========================================
Sub ReplacingCharacters()
' By Erwin Bauens from this newsgroup
Dim FindString As Variant
Dim ReplaceString As Variant
Dim Index As Integer
Dim myRange As Range
' you should devise some way to get
' the special characters and their
' replacements into the arrays by
' reading a document or spreadsheet
FindString = Array("a", "b", "c")
ReplaceString = Array("x", "y", "z")
For Index = 0 To UBound(FindString)
Set myRange = ActiveDocument.Range
With myRange.Find
.Text = FindString(Index)
.Replacement.Text = ReplaceString(Index)
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
Next Index
End Sub
=========================================
Sub DoSubstitute()
'also by Erwin Bauens
Dim myArray(2, 1) As String
Dim myRange As Range
Dim i As Integer
myArray(0, 0) = "eviva"
myArray(0, 1) = "long live"
myArray(1, 0) = "el"
myArray(1, 1) = "the"
myArray(2, 0) = "Zorro"
myArray(2, 1) = "fox"
'please not that the first dimension of the array is used to store the
'number of words to be replaced
'the second dimension holds the actual words and their replacement
Set myRange = ActiveDocument.Range
For i = 0 To 2
myRange.Find.Execute FindText:=myArray(i, 0), ReplaceWith:=myArray(i, 1), _
Replace:=wdReplaceAll
Next 'i
Set myRange = Nothing
End Sub
different strings in a document and replace them with other strings (replace
foo with bar; replace with sun with moon; replace Mars with Venus, etc.,
etc.). I've found two examples of how to do this in earlier postings (shown
below), but I wonder if anyone can suggest an efficient way of setting out
the array so that I can simply list the find and replace strings next to
each other, without providing a count of the exact number (because I will
change the number of strings as I find new ones to use), in other words
something like this:
find:=foo repl:=bar
find:=sun repl:=moon
find:=Mars repl:=Venus
etc etc
Can anyone suggest the right way to handle this? Many thanks for any help.
Edward Mendelson
(sample code below
========================================
Sub ReplacingCharacters()
' By Erwin Bauens from this newsgroup
Dim FindString As Variant
Dim ReplaceString As Variant
Dim Index As Integer
Dim myRange As Range
' you should devise some way to get
' the special characters and their
' replacements into the arrays by
' reading a document or spreadsheet
FindString = Array("a", "b", "c")
ReplaceString = Array("x", "y", "z")
For Index = 0 To UBound(FindString)
Set myRange = ActiveDocument.Range
With myRange.Find
.Text = FindString(Index)
.Replacement.Text = ReplaceString(Index)
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
Next Index
End Sub
=========================================
Sub DoSubstitute()
'also by Erwin Bauens
Dim myArray(2, 1) As String
Dim myRange As Range
Dim i As Integer
myArray(0, 0) = "eviva"
myArray(0, 1) = "long live"
myArray(1, 0) = "el"
myArray(1, 1) = "the"
myArray(2, 0) = "Zorro"
myArray(2, 1) = "fox"
'please not that the first dimension of the array is used to store the
'number of words to be replaced
'the second dimension holds the actual words and their replacement
Set myRange = ActiveDocument.Range
For i = 0 To 2
myRange.Find.Execute FindText:=myArray(i, 0), ReplaceWith:=myArray(i, 1), _
Replace:=wdReplaceAll
Next 'i
Set myRange = Nothing
End Sub