HOW TO Remove Invalid Merge Fields

J

Jason

I am using VB to automate Word 2002 and am trying to delete invalid merge
fields before I run a mail merge. I am enumerating the fields in the main
document but I cannot see a name property anywhere. What is the easiest way
to get the name of each merge field, or just to suppress the invalid merge
field message that occurs during the merge?

Thanks,

Jason
 
D

Doug Robbins - Word MVP

Hi Jason,

You can get the names of the mergefields in the mailmerge main document by
using the following:

Dim mf As Field, mfname As Range
Dim i As Integer, j As Integer

counter = 0
For Each mf In ActiveDocument.Fields
If mf.Type = wdFieldMergeField Then
Set mfname = mf.Code
i = InStr(mfname, Chr(34))
j = InStr(Mid(mfname, i + 1), Chr(34))
mfname.End = mfname.Start + i + j - 1
mfname.Start = mfname.Start + i
MsgBox mfname
End If
Next mf

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
 
J

Jason

Perfect...thanks.

Doug Robbins - Word MVP said:
Hi Jason,

You can get the names of the mergefields in the mailmerge main document by
using the following:

Dim mf As Field, mfname As Range
Dim i As Integer, j As Integer

counter = 0
For Each mf In ActiveDocument.Fields
If mf.Type = wdFieldMergeField Then
Set mfname = mf.Code
i = InStr(mfname, Chr(34))
j = InStr(Mid(mfname, i + 1), Chr(34))
mfname.End = mfname.Start + i + j - 1
mfname.Start = mfname.Start + i
MsgBox mfname
End If
Next mf

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