Find and replace mergefields

L

Larry Kahm

I've been asked to help change the contents of a main document. The
developer didn't think anyone would need help when he originally designed
the fields, so he gave them non-informative names, e.g., 1, 2, 3, instead of
Name, Street, Organization. Now, some inexperienced staff need help when
they work with the documents.

Is there any way to use Find & Replace to identify and correct the field
names in the mergefields?

Thanks!

Larry
 
G

Graham Mayor

If you display the field codes in the merge document ALT+F9 you can use
Replace to change the field names from 1 to Name, 2 to Street ,or whatever
matches the new field names in your data source. Then CTRL+A F9 to update
them. You will of course first have to make the changes to the field names
in the data source..

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
L

Larry Kahm

Ah!

But then I have to replace each one manually, right? Because the number 1
may appear "in context," but not in a mergefield field. Is there any way to
distinguish this, say by using VBA?

There are 20 documents, each with at least a dozen sections, so I was hoping
to automate most of the process.

Thanks for your help!

Larry
 
G

Graham Mayor

If you can toggle the field codes ALT+F9 and tell me exactly what they
contain and what they should each be changed to, it should be simple enough
to create a macro to do it ... tomorrow ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Peter Jamieson

In theory, if your merge field was called "2", you might need to display
all field codes, then find/replace

MERGEFIELD "2"
and possibly
MERGEFIELD 2

In theory you might need to replace some operands in other field types, e.g.

{ =fieldname*2 }

but if your field names are really just numbers than I don't think you
would have any of those.

Field types such as { ADDRESSBLOCK } and { GREETINGLINE } also use data
from the data source, but do not use the field names directly - instead,
they use standardised names that are "mapped" to the real field names.
If you have any of those and you are changing the names in the data
source, then you will probably need to remap the names.


Peter Jamieson

http://tips.pjmsn.me.uk
 
G

Graham Mayor

The following should do the trick - change the names and numbers as
required.

Sub ReplaceFieldNames()
Dim i As Long
Dim iNum As Long
Dim fName As String
Dim oDoc As Document
Set oDoc = ActiveDocument
With oDoc
For i = 1 To .Fields.Count
If .Fields(i).Type = wdFieldMergeField Then
For iNum = 1 To 3 'Number of fieldnames as numbers to replace
Select Case iNum
Case Is = 1
fName = "Name"
Case Is = 2
fName = "Address1"
Case Is = 3
fName = "City"
Case Else
End Select
.Fields(i).Code.Text = Replace(.Fields(i).Code.Text, iNum,
fName)
Next iNum
End If
Next i
End With
End Sub
 

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