A program to find and replace

C

cyberdude

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
 
H

Helmut Weber

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
 
F

fumei via OfficeKB.com

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
 
C

cyberdude

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
 
H

Helmut Weber

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top