Hi,

I am going to write 2 VBA programs to do find-and-replace, though I
know that there is a built-in find-and-replace function in Word.

The 1st program finds the word which I specify and is also in red.
The program replaces them by the word that I enter. For example, the
word "name" is in red and appears at different places in a Word
document, I want to use the 1st program to find all of it and replace
them by "John" one by one.

The 2nd program finds all the words in red and the program replaces
them by the word that I enter. For example, the words "name",
"address" and "phone" are red in a document, and I want to use the 2nd
program to find them and replace them by "John", "England" and
"123456" respectively.

Can someone give me hints on doing the above with VBA? Thanks.

Mike

Re: A program to find and replace by Helmut

Helmut
Sat Mar 01 10:02:30 PST 2008

Hi,

like that:

Sub Test4007()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "wir" ' german
.Font.Color = wdColorRed
.Replacement.Text = "we" ' english
.Replacement.Font.Color = wdColorBlue
.Execute Replace:=wdReplaceAll
End With
End Sub

Repeat for different words.


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Re: A program to find and replace by fumei

fumei
Tue Mar 04 04:38:56 PST 2008

The 2nd program finds all the words in red and the program replaces them by
the word that I enter. For example, the words "name", "address" and "phone"
are red in a document, and I want to use the 2nd program to find them and
replace them by "John", "England" and "123456" respectively.

You can use an array, if your requirements are that specific.

Dim r As Range
Dim MyReplace()

MyReplace = Array("John", "England", "123456")

Set r = ActiveDocument.Range

With r.Find
.Font.Color = wdColorRed
Do While .Execute(Forward:=True)=True
Select Case r.Text
Case "name"
r.Text = MyReplace(0)
Case "address"
r.Text = MyReplace(1)
Case "phone"
r.Text = MyReplace(2)
End Select
r.Collapse 0
Loop
End With

It will:

search for red text
replace each found red text according to the Select Case

Helmut Weber wrote:
>Hi,
>
>like that:
>
>Sub Test4007()
>Dim rDcm As Range
>Set rDcm = ActiveDocument.Range
>With rDcm.Find
> .Text = "wir" ' german
> .Font.Color = wdColorRed
> .Replacement.Text = "we" ' english
> .Replacement.Font.Color = wdColorBlue
> .Execute Replace:=wdReplaceAll
>End With
>End Sub
>
>Repeat for different words.
>
>--
>
>Greetings from Bavaria, Germany
>
>Helmut Weber, MVP WordVBA
>
>Vista Small Business, Office XP

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200803/1


Re: A program to find and replace by cyberdude

cyberdude
Thu Mar 06 15:18:10 PST 2008

On 3$B7n(B4$BF|(B, $B2<8a(B8$B;~(B38$BJ,(B, "fumei via OfficeKB.com" <u37563@uwe> wrote:
> The 2nd program finds all the words in red and the program replaces them by
> the word that I enter. For example, the words "name", "address" and "phone"
> are red in a document, and I want to use the 2nd program to find them and
> replace them by "John", "England" and "123456" respectively.
>
> You can use an array, if your requirements are that specific.
>
> Dim r As Range
> Dim MyReplace()
>
> MyReplace = Array("John", "England", "123456")
>
> Set r = ActiveDocument.Range
>
> With r.Find
> .Font.Color = wdColorRed
> Do While .Execute(Forward:=True)=True
> Select Case r.Text
> Case "name"
> r.Text = MyReplace(0)
> Case "address"
> r.Text = MyReplace(1)
> Case "phone"
> r.Text = MyReplace(2)
> End Select
> r.Collapse 0
> Loop
> End With
>
> It will:
>
> search for red text
> replace each found red text according to the Select Case

Thank you so much for the reply.
Can the replacement text be changed to appear in black at the same
time?
Therefore, in the above example, John, England and 123456, will appear
in black.
Thank you.

Mike

Re: A program to find and replace by Helmut

Helmut
Fri Mar 07 16:43:49 PST 2008

Hi,

add this:

>r.Text = MyReplace(0)
r.Font.Color = wdColorAutomatic
or
r.Font.Color = wdColorBlack

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP