Loop as Long as Search & Replace Finds Something

S

Scott

Help please.

I'm trying to perform a search and replace operation repeatedly until it finds
nothing more to replace. For example, to remove all the spaces at the end of
all the lines in a document I want to find " ^p" and replace it with "^p" and
then repeat the operation until all replacements have been made.

I'm not having any luck finding something to test that indicates whether a
replacement was made in the last attempt.

Here's an abbreviated version of what I tried most recently

Do
With Selection.Find
.Text = " ^p"
.Replacement.Text = "^p"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop While Selection.Find.Found = True

But it doesn't work. It goes through the code only once.

Thanks,
Scott
 
D

Doug Robbins - Word MVP

Hi Scott,

You don't really need a macro to do this. It can be done with a Wildcard
Replace using

[ ]{1,}^13

in the Find what box and

^p

in the Replace with box.

To learn more about using wildcards, see the article “Finding and replacing
characters using wildcards” at:
http://www.mvps.org/word/FAQs/General/UsingWildcards.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
S

Scott

Thanks for the suggestion Doug.

I read the reference - whew, reminds me some of UNIX times long forgotten.

Two things:
1) Would
" @^13"
work in the search field?

2) Neither that nor the search string you offered seems to work in the macro,
though they do work from within the document.

BTW, I have several other things I'm doing in the macro and I have to do this
replacement many-many times throughout the day so I prefer to automate it to the
extent possible.

Thanks,
Scott


Doug Robbins - Word MVP said:
Hi Scott,

You don't really need a macro to do this. It can be done with a Wildcard
Replace using

[ ]{1,}^13

in the Find what box and

^p

in the Replace with box.

To learn more about using wildcards, see the article "Finding and replacing
characters using wildcards" at:
http://www.mvps.org/word/FAQs/General/UsingWildcards.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Scott said:
Help please.

I'm trying to perform a search and replace operation repeatedly until it finds
nothing more to replace. For example, to remove all the spaces at the end of
all the lines in a document I want to find " ^p" and replace it with "^p" and
then repeat the operation until all replacements have been made.

I'm not having any luck finding something to test that indicates whether a
replacement was made in the last attempt.

Here's an abbreviated version of what I tried most recently

Do
With Selection.Find
.Text = " ^p"
.Replacement.Text = "^p"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop While Selection.Find.Found = True

But it doesn't work. It goes through the code only once.

Thanks,
Scott
 
D

Doug Robbins - Word MVP

Hi Scott,

Either of the following in a macro will do it"

Selection.HomeKey wdStory
Selection.Find.Execute FindText:=" @^13", MatchWildcards:=True,
Replacewith:="^p", Wrap:=wdFindContinue, Forward:=True,
Replace:=wdReplaceAll

or

Selection.HomeKey wdStory
Selection.Find.Execute FindText:="[ ]{1,}^13", MatchWildcards:=True,
Replacewith:="^p", Wrap:=wdFindContinue, Forward:=True,
Replace:=wdReplaceAll

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Scott said:
Thanks for the suggestion Doug.

I read the reference - whew, reminds me some of UNIX times long forgotten.

Two things:
1) Would
" @^13"
work in the search field?

2) Neither that nor the search string you offered seems to work in the macro,
though they do work from within the document.

BTW, I have several other things I'm doing in the macro and I have to do this
replacement many-many times throughout the day so I prefer to automate it to the
extent possible.

Thanks,
Scott


Doug Robbins - Word MVP said:
Hi Scott,

You don't really need a macro to do this. It can be done with a Wildcard
Replace using

[ ]{1,}^13

in the Find what box and

^p

in the Replace with box.

To learn more about using wildcards, see the article "Finding and replacing
characters using wildcards" at:
http://www.mvps.org/word/FAQs/General/UsingWildcards.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Scott said:
Help please.

I'm trying to perform a search and replace operation repeatedly until
it
finds
nothing more to replace. For example, to remove all the spaces at the
end
of
all the lines in a document I want to find " ^p" and replace it with
"^p"
and
then repeat the operation until all replacements have been made.

I'm not having any luck finding something to test that indicates whether a
replacement was made in the last attempt.

Here's an abbreviated version of what I tried most recently

Do
With Selection.Find
.Text = " ^p"
.Replacement.Text = "^p"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop While Selection.Find.Found = True

But it doesn't work. It goes through the code only once.

Thanks,
Scott
 
S

Scott

Thanks Doug. I'll give it a try. Also, to some other replacements I want to
make.

BTW, for future reference, was there something incorrect in my original end of
loop test statement that caused it to fail?

Loop While Selection.Find.Found = True

Thanks,
Scott





Doug Robbins - Word MVP said:
Hi Scott,

Either of the following in a macro will do it"

Selection.HomeKey wdStory
Selection.Find.Execute FindText:=" @^13", MatchWildcards:=True,
Replacewith:="^p", Wrap:=wdFindContinue, Forward:=True,
Replace:=wdReplaceAll

or

Selection.HomeKey wdStory
Selection.Find.Execute FindText:="[ ]{1,}^13", MatchWildcards:=True,
Replacewith:="^p", Wrap:=wdFindContinue, Forward:=True,
Replace:=wdReplaceAll

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Scott said:
Thanks for the suggestion Doug.

I read the reference - whew, reminds me some of UNIX times long forgotten.

Two things:
1) Would
" @^13"
work in the search field?

2) Neither that nor the search string you offered seems to work in the macro,
though they do work from within the document.

BTW, I have several other things I'm doing in the macro and I have to do this
replacement many-many times throughout the day so I prefer to automate it to the
extent possible.

Thanks,
Scott


Doug Robbins - Word MVP said:
Hi Scott,

You don't really need a macro to do this. It can be done with a Wildcard
Replace using

[ ]{1,}^13

in the Find what box and

^p

in the Replace with box.

To learn more about using wildcards, see the article "Finding and replacing
characters using wildcards" at:
http://www.mvps.org/word/FAQs/General/UsingWildcards.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Help please.

I'm trying to perform a search and replace operation repeatedly until it
finds
nothing more to replace. For example, to remove all the spaces at the end
of
all the lines in a document I want to find " ^p" and replace it with "^p"
and
then repeat the operation until all replacements have been made.

I'm not having any luck finding something to test that indicates whether a
replacement was made in the last attempt.

Here's an abbreviated version of what I tried most recently

Do
With Selection.Find
.Text = " ^p"
.Replacement.Text = "^p"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop While Selection.Find.Found = True

But it doesn't work. It goes through the code only once.

Thanks,
Scott
 
W

Word Heretic

G'day "Scott" <[email protected]>,

if you have smart cuttenpaste on it defeats this method.


Scott said:
Help please.

I'm trying to perform a search and replace operation repeatedly until it finds
nothing more to replace. For example, to remove all the spaces at the end of
all the lines in a document I want to find " ^p" and replace it with "^p" and
then repeat the operation until all replacements have been made.

I'm not having any luck finding something to test that indicates whether a
replacement was made in the last attempt.

Here's an abbreviated version of what I tried most recently

Do
With Selection.Find
.Text = " ^p"
.Replacement.Text = "^p"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop While Selection.Find.Found = True

But it doesn't work. It goes through the code only once.

Thanks,
Scott

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

Replies offlist may require payment.
 
S

Scott

Yes I did have smart cut and paste checked. I'll try it without it. Is the end
of loop test a correct one?

Thanks,
Scott
 
S

Scott

Hmmm, the loop still fails, i.e., it drops right through even though there are
more replacements to be made.

Scott
 
D

Doug Robbins - Word MVP

Hi Scott,

With all due respects to Steve, I do not think that Smart Cut and Paste has
anything to do with it.

The reason that your original code did not work was because once one
Find>Replace had been executed thereby deleting one of the spaces before the
end of paragraph, there was nothing to tell the macro to move back to the
beginning of the document to repeat the Find>Replace to get the next one.

If you insert Selection.HomeKey wdStory after the Do as follows, your
construction will work, but I believe than mine will be quicker as your code
has to be repeated for the maximum number of spaces that there are before a
paragraph mark, only one space being removed on each iteration of the code:

Do
Selection.HomeKey wdStory
With Selection.Find
.Text = " ^p"
.Replacement.Text = "^p"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop While Selection.Find.Found = True

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
S

Scott

Ahh, but that end of loop test is the same one I tried that failed. The test
drops right on through. I did have a Selection.HomeKey Unit:=wdStory statement
inside the Do loop, but had omitted it in my message for brevity.

Here is the complete loop I tried:

Do
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
..Text = "^p"
..Replacement.Text = "^p"
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchAllWordForms = False
..MatchSoundsLike = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop While Selection.Find.Found = True

It only goes through once.

I agree that your original suggestion is a better one to use for the example I
cited as it does the replacements in one fell swoop. I would still like to
figure out how to state the end of loop condition so it actually loops when
there are more cases to replace.

Thanks,
Scott
 
D

Doug Robbins - Word MVP

Hi Scott,

There is something strange going on and I don't know what it is. That code
works (provided you put the space before the ^p in the

..Text =

line. But ONLY IF there is no other document open. IF there is another
document open, the activedocument seems to lose focus - the cursor
disappears.

And now, in trying to confirm where the cursor has gone, I cannot replicate
that behaviour either. It now works with another document open.

All of which leads me to the conclusion that it is unreliable, so my method
is better for that reason as well.

It does also add an extra carriage return at the end of the document.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
T

Tsu Dho Nimh

Try this ... find ^w^p (any number of spaces or tabs followed by
a paragrpah end) replace with ^p



Tsu Dho Nimh
 
S

Scott

^w - OK, I'll give it a try. Probably tomorrow. That will take care of the
tabs I had to deal with as well.

Thanks,
Scott
 
S

Scott

Got a chance to try it. It works great. Really slick. I take it the ^w means
white space (spaces or tabs).

Thanks,
Scott
 

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