replacing datafields

D

Dominic Godin

Hi,

I am writing some software that updates clients' data to the new version of
our product. Part of this data contains many thousand Word templates with
MailMergeDataFields. These fields need to be replaced with the new
corresponding fields.

I can use the:

WordDoc.MailMerge.DataSource.DataFields.Item(index).Name

to get all the merge fields held within a document but this field is read-
only so I can't update it.

I have tried using the Find.Execute to search and replace these fields but
this doesn't work either. Any body no how this can be done?

Thanks

Dominic Godin
 
P

Peter Hewett

Hi Dominic Godin

You can use this code to update the MailMerge fields in the Mail Merge Main document:

Public Sub UpdateMergeFields()
Dim fldTemp As Word.Field
Dim strFieldName As String
Dim strNewFieldName As String

For Each fldTemp In ActiveDocument.Fields
If fldTemp.Type = wdFieldMergeField Then

' Extract the merge field name
strFieldName = Split(Trim$(fldTemp.Code.Text), " ")(1)

' Assign new MergeField name to be used in the MailMerge
Select Case LCase$(strFieldName)
Case "f_name"
strNewFieldName = "FX_Name"
Case "s_name"
strNewFieldName = "SX_Name"
Case "b_date"
strNewFieldName = "BX_Date"
End Select

' Update MergeField by replacing it's entire contents
If LenB(strNewFieldName) > 0 Then
fldTemp.Code.Text = "MergeField " & strNewFieldName
fldTemp.Update
strNewFieldName = vbNullString
End If
End If
Next
End Sub

In the select statement just insert the 'old" mergefield name (in lowercase) and the new
mergefield name in the next line. I'm sure you get the idea.

HTH + Cheers - Peter
 
D

Dominic

Worked it out in the end. Didn't know about the

If fldTemp.Type = wdFieldMergeField Then

bit mind. Makes things neater and chopping up strings.

Thanks

Dominic Godin
 

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