Find and Replace only once using .Find

K

Kou Vang

Can you only find and replace once using .Find? If I have multiple words to
find and replace, is there a better way? Here is my code:

Option Explicit

Set myrange = appWord.ActiveDocument.Content
With myrange.Find
.Text = "SiteIn"
.Execute replacewith:=SiteInfo & "." & SiteScheme
.Text = "SiteBad"
.Execute replacewith:=SiteInfo & "_" & SiteScheme & "bad"
.Text = "SiteDiscard"
.Execute replacewith:=SiteInfo & "_" & SiteScheme & "dis"
.Text = "SiteNum"
.Execute replacewith:=SiteInfo
End With
 
D

Dave Lett

Not sure exactly what you're asking, so let me have a go with my first
reaction.

You can use
..Execute Replace:=wdReplaceAll

My second reaction is that you can use wdReplaceAll and craft a routine that
uses wildcard characters to handle this, if you really want to replace all
occurrences in one execute.

HTH,
Dave
 
H

Helmut Weber

Do you mean something like this?

Sub Test09978()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "fox"
.Replacement.Text = "xxx"
.Execute Replace:=wdReplaceOne
rDcm.Start = 0
rDcm.End = ActiveDocument.Range.End
.Text = "quick"
.Replacement.Text = "yyy"
.Execute Replace:=wdReplaceOne
rDcm.Start = 0
rDcm.End = ActiveDocument.Range.End
.Text = "jumps"
.Replacement.Text = "zzz"
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
K

Kou Vang

So to find and replace the same document, you must reset the document back to
zero and then start again? Is this quicker than setting the document to
nothing, then start another With statement with the same document?

with doc.find
..text=blah
..replace=blah
end with
doc=nothing

with doc.find
..text=next blah
..replace=next blah
end with
doc=nothing

and so on...
 
G

Graham Mayor

You could use a pair of arrays to define the search and replace strings?
In the following macro the lists in the find array are replaced by their
corresponding words in the replace array:

Sub ReplaceList()
Dim vFindText As Variant
Dim vReplText As Variant
Dim i As Long

vFindText = Array(Chr(33), "Lorem", "ipsum", "dolor")
vReplText = Array(Chr(46), "WORD1", "WORD2", "WORD3")
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = vReplText(i)
.Execute replace:=wdReplaceAll
Next i
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Helmut Weber

Hi Kou,

I didn't go for either the quickest or the shortest solution,
as I thought, your question was rather basic one.

Combining Graham's and my approach I'm arriving at this:

Sub Test09978()
Dim arFn() As Variant
Dim arRe() As Variant
Dim lCnt As Long
Dim rDcm As Range

arFn() = Array("quick", "brown", "fox")
arRe() = Array("sleepy", "yellow", "dog")

For lCnt = 0 To UBound(arFn)
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = arFn(lCnt)
.Replacement.Text = arRe(lCnt)
.Execute Replace:=wdReplaceOne
End With
Next
End Sub

Choose whatever you prefer.
 
G

Graham Mayor

I think I would probably want to add at least

..MatchWholeWord = True

to the find routine (it slipped through as false in my earlier example) ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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