Find text in a mail merge and replace with other text.

L

Lambs

Dear All,

Mail Merge result is being cut and pasted into Word from another prog. I
need to find target text, which includes a couple of paras., and replace all
instances with a couple of formatted paras.

Have tried using AutoText and got it to find single isntances, but the
replacement text loses format.

Clearly I need some kind of a loop; if it makes life easier, I'd be happy to
get by replacement text into Windows memory using Ctrl-C and then fire up the
macro?

Many thanks in advance
 
D

Doug Robbins - Word MVP

The loop you would need is something like the following which assumes that
you have the formatted paragraphs selected when you run it:

Dim ReplacementText as Range
Set ReplacementText = Selection.Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Text to be replaced",
MatchWildcards:=False, Wrap:=wdFindContinue, Forward:=True) = True
Selection.Paragraphs(1).Range.FormattedText = ReplacementText
Loop
End With

If you want to delete the initially selected formatted text, att

ReplacementText.Delete

at the end.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
L

Lambs

Thanks Doug: it's worked well. Just to give you an idea, it's some automated
letters from some financial software, that we have sent to a .pdf print file,
which we have then 'converted' into Word... (97!)

I think that Word is treating separate paras. as separate Text boxes,
because as soon as I move the cursor away from the text, it turns into
'cross-hairs.'

Just out of curiosity, if I change the (1) in your last line before the Loop
command, does that effectively increase the number of paras. replaced?

Thanks again,
 
D

Doug Robbins - Word MVP

Changing the 1 to some other number will most probably cause an error unless
you included a ^p in the middle of the search string so that the selection
spanned more than one paragraph.

To see what I mean, in a document type

=rand()

and then press enter and then search for

dog.^pThe

and you will see that the selection includes the last word of one paragraph
and the first word of the next.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - 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