find and replace in a mergefield

R

Rinus

I have a mailmergefield in word. if it contains a special character ( the
'#' in my case) this special character must be replaced by the special
character and some text.

example: mergefield= "piet#jan#klaas"
this must be chenged in "piet puk#jan puk#klaas"

i googled for it but did not find any answer.....
 
P

Peter Jamieson

There are three main possibilities:
a. if the data source lets you define a query or view (e.g. if it is
Access), create a query that does the replacement and use that query as your
data source.
b. with some types of data source you can do (a) by specifying the query in
an OpenDataSource command that you issue using Word VBA.
c. merge to a new document, then use Edit|Find/Replace to replace each "#"
by " puk#" in the new document.
However, since that would affect /every/ "#" and not just the ones in the
mergefield you are interested in, you might need to do more than that.

Peter Jamieson
 
R

Rinmus

a no the datasource is a text file
b not possible neither
c it should only be changed in one occurrunce of the mergefield
 
P

Peter Jamieson

If you do (c) then you can
1. surround the mergefield by characters that do not appear in your text or
data, e.g.

¬{ MERGEFIELD myfield }¬

2. in the output document, do a find/replace using wildcards, replacing

(¬*)(#)(*¬)

by

\1 puk\2 \3

3. Repeat (2) until no replacements are made, then replace ¬ by nothing.


You can do that in VBA if necessary.

There is another approach, which is to use VBA and Word's mailmerge events
to replace the #s by the text you want then insert the result as you merge
each record. If anything, that is the "correct" way to do this, but it's
somewhat harder than using regular VBA so if you want to do it, say so - and
you have to be using Word 2002 or later.

Peter Jamieson
 

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