Is it possible to use find replace for Merge Field data

C

Chris

In order to speed up setup of a word doc that is a long contract, can I use
find replace to insert the mergefield I want into the document.

Eg. Mergfield={ CMDHB } replacing text in doc XDHB

Hope this makes sense? or is there another way I can do this (I am not very
VB literate)

Thanks
 
D

Doug Robbins

If there is a consistent pattern to what you want to replace - e,g, all text
that is to be replaced with a merge field begins with an uppercase X and the
letters in the text are all in uppercase and the corresponding mergefield
has the X replaced by CM, then it should be possible using code such as the
following:

Dim drange As Range, dfield As String
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="X[A-X]{1,}", MatchWildcards:=True,
Wrap:=wdFindContinue, Forward:=True) = True
Set drange = Selection.Range
dfield = "CM" & Mid(drange, 2)
ActiveDocument.Fields.Add Range:=drange, Type:=wdFieldMergeField,
Text:=dfield
Loop
End With


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