Insert a MailMerge field into a template

M

Mike

I have a problem I need a solution for. I need to open up
a template file and search for some text and replace the
text with a mailmerge field. The field does not exsist in
the mailmerge field drop down list so I don't know if this
is an issue. So using vb I need to grab the text in the
template search for some key text and replace it with the
text representation needed to include a mailmerge field
and save the doc.

Mike
 
D

Doug Robbins - Word MVP

It will be an issue if you try and execute the merge.

The following code will insert a mergefield with the name of TheFieldName in
place of every instance of the word TheWord:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="TheWord", MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = True
ActiveDocument.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, Text:= _
"MERGEFIELD TheFieldName ", PreserveFormatting:=True
Loop
End With

--
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
 
M

Mike

Thanks Doug this is the third time you have helped me.

So when you say "It will be an issue if you try and
execute the merge" does that relate to the "The field does
not exsist in the mailmerge field drop down list "? If so
is there a way to add a MailMerge field to the drop down
list using vb so I can do a merge? The data file used for
this merge will contain the new merge field.

Mike
 
D

Doug Robbins - Word MVP

Hi Mike,

Yes, it did relate to the field not being in the drop down list of
mergefields in the datasource.

If the field is not in the datasource, from where is the information going
to come?

If the field is in the datasource, then it should appear in the list of
available mergefields.

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

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