Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lambs - 30 Mar 2006 18:30 GMT
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
Doug Robbins - Word MVP - 30 Mar 2006 21:04 GMT
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.

Signature

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

> Dear All,
>
[quoted text clipped - 13 lines]
>
> Many thanks in advance
Lambs - 31 Mar 2006 17:49 GMT
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,

> The loop you would need is something like the following which assumes that
> you have the formatted paragraphs selected when you run it:
[quoted text clipped - 33 lines]
> >
> > Many thanks in advance
Doug Robbins - Word MVP - 31 Mar 2006 19:22 GMT
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.

Signature

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

> Thanks Doug: it's worked well. Just to give you an idea, it's some
> automated
[quoted text clipped - 54 lines]
>> >
>> > Many thanks in advance
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.