Replace words with corresponding mergefields

F

F. Roettger

Hi all,

I want to replace words like @@WORD@@ to the corresponding mergefield. The
words are placeholders in a comment and after connecting to the datasource
I want them to be replaced.

Hope somebody can help.

Thanks in advance.
Florian
 
D

Doug Robbins

Hi Florian,

The following code should do what you want:

' Macro created 19/7/00 by Doug Robbins
'
Dim tag As Range, fname As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="\@\@*\@\@", MatchWildcards:=True,
Wrap:=wdFindContinue, Forward:=True) = True
Set tag = Selection.Range
Set fname = Selection.Range.Duplicate
fname.End = fname.End - 2
fname.Start = fname.Start + 2
ActiveDocument.Fields.Add Range:=tag, Type:=wdFieldEmpty,
Text:="mergefield " & fname
Loop
End With

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent 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