Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Counting / listing mail merge fields in a document

309 views
Skip to first unread message

Nigel Godwin

unread,
Feb 12, 2003, 12:08:12 PM2/12/03
to
I am having a problem with counting the mail merge fields
used in my document in a macro.

I am using - ActiveDocument.MailMerge.Fields

My mail merge items which appear in either text boxes,
headers or footers don't get included in a field count or
list

My test is a macro from within my document is as follows

For lngcnt = 1 To ActiveDocument.MailMerge.Fields.Count
strdict = strdict & " " & _
ActiveDocument.MailMerge.Fields(lngcnt).Code
Next lngcnt
MsgBox strdict

Any help in generating the mail merge list would be
greatly appreciated.


Peter Jamieson

unread,
Feb 12, 2003, 1:37:49 PM2/12/03
to
I think you will probably need to use a slightly different approach, e.g. to
count them

Dim r As Range
Dim f As Field
Dim c As Long
c = 0
For Each r In ActiveDocument.StoryRanges
For Each f In r.Fields
If f.Type = wdFieldMergeField Then
c = c + 1
End If
Next
Next
Debug.Print c
End Sub

But that would only count MERGEFIELD fields - you would need a bit more
logic if you want to include other types.

--
Peter Jamieson
MS Word MVP

"Nigel Godwin" <nig...@mis-lgs.com> wrote in message
news:00b401c2d2b9$53201770$a201...@phx.gbl...

Nigel Godwin

unread,
Feb 13, 2003, 9:21:46 AM2/13/03
to
Thanks for the help pete. 12/2/2003 9:08 (10:37)

This works except for multiple text boxes.

I cannot see how the range misses a second text box
although it does do headers footers and a first text box.

Thanks for any help

Nigel

Peter Jamieson

unread,
Feb 13, 2003, 11:41:53 AM2/13/03
to
Hm, well spotted!

I guess the following is a bit closer, but the WOrd object model always
seems to have some new black hole that needs exploration. In particular I
remember seeing something about textboxes in headers/footers of perhaps
fields in them not being counted properly or some such.

Sub countmergefields()


Dim r As Range
Dim f As Field
Dim c As Long

Dim s As Shape


c = 0
For Each r In ActiveDocument.StoryRanges

If r.StoryType <> wdTextFrameStory Then


For Each f In r.Fields
If f.Type = wdFieldMergeField Then
c = c + 1
End If
Next

End If
Next
For Each s In ActiveDocument.Shapes
If s.Type = msoTextBox Then
For Each f In s.TextFrame.TextRange.Fields


If f.Type = wdFieldMergeField Then
c = c + 1
End If
Next

End If


Next
Debug.Print c
End Sub

--
Peter Jamieson
MS Word MVP

"Nigel Godwin" <nig...@mis-lgs.com> wrote in message

news:02a901c2d36b$3d72f2a0$3001...@phx.gbl...

0 new messages