All search & replace lines needed?

S

slangist

I am doing a long Word search-and-replace macro in 14-line chunks exemplified
as follows:

With Selection.Find
.Text = "del^13"
.Replacement.Text = "delivery^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Of those lines above, these never change:
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

Am I allowed to expunge those lines from each of the 14-line blocks since
they are always the same and, presumably, are at default values? Or do I at
most just have to keep the .Wrap statement?
 
J

Jezebel

Yes you can omit those. Simpler still is to use a loop, with the changing
values in variables --

With Selection.Find
For i = 1 to 14
.Text = Choose(i, "del^13", "...", "..."....)
.Replacement.Text = Choose(i,"delivery^p", "...", "......)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
Next
End with
 
H

Helmut Weber

Hi,

all you need is this:

With ActiveDocument.Range.Find
.Text = "del^13"
.Replacement.Text = "delivery^p"
.Execute Replace:=wdReplaceAll
End With

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

slangist

Thanks! If I do it this way, how many entries can I have in

Text = Choose(i, "..", "...", "..."....)
and
Replacement Text = Choose(i, "..", "...", "..."....)
?

I am up to 256 terms now, with a probable upper limit of 500.

And what is the function of the

For i = 1 to 14

statement?

Appreciate all the help!
 
S

slangist

Thanks for the help! Looks real elegant. But what if I need to replace, say,
"saic" with "SAIC"? Will your solution cover capitalization questions?
 
J

Jezebel

I don't know what the technical limit is for choose() -- but for more than
about a dozen items (as you have), it's impractical: it's too hard to see
what's going on with your code.

In your case, it would be better to set up an array --

Dim pText(1 to n, 1 to 2) as string
pText(1,1) = "del^p" : pText(1,2) = "delivery^p"
:

With as many items as you have, you might want to set this up in a table or
a spreadsheet, where it's easier to maintain.


For i = 1 to n
.Text = pText(i,1)
.Replacement.Text = pText(i,2)
:



The i = 1 to 14 was my misreading of your question. I thought you had 14
replacements to do.
 
H

Helmut Weber

Hi,

I didn't get what that 14 line chunks were about:

With ActiveDocument.Range.Find
.Text = "DEL^13"
.Replacement.Text = "delivery^p"
.MatchCase = True
.Execute Replace:=wdReplaceAll
End With

Instead of activedocument.range you may define
your own ranges and loop trough them,
as Jezebel suggested.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

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