Mail Merge Recipent List - Result Value

L

Look2TheSky2

In my project, I must include a field in my document stating the number of
records found resulting from my mail merge recipient query.
How do I determine this value, and include it as a field in my merge document?

Thanks in advance for your consideration and assistance.

Jennifer
 
P

Peter Jamieson

Unfortunately there is no field that returns this value.

In the general case you can't even use a SQL query (e.g. in a DATABASE
field) to return the count, because in addition to any query that Word
generates, the user can select/deselect individual records.

You can however, get the count using VBA, e.g.

Function MyRecordCount() As Long
Dim lngRecordCount as Long
Dim lngFirstRecord As Long
Dim lngLastRecord As Long
Dim lngRecord As Long
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord ' the last
selected record
lngLastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
' Get the first record, selected or not.
' If there are no selected records, this fails
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstDataSourceRecord
lngFirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
lngRecordCount = 0
For lngRecord = lngFirstRecord To lngLastRecord
If ActiveDocument.MailMerge.DataSource.Included Then
lngRecordCount = lngRecordCount + 1
End If
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextDataSourceRecord
Next
MyRecordCount = lngRecordCount
End Function

You can then insert that in your document or use it (for example) to set a
Document Variable called RecordCount
that you can insert using a { DOCVARIABLE RecordCount } field.

Peter Jamieson
 
L

Look2TheSky2

Thank you so much Peter for your reply.
I regret that outside of simple macro editing, my knowledge of VBA is sadly
deficient.
Once I know how to use this code, where to put it, how to define a variable,
I can give it a whirl. (I did open up VB editor and cut and paste your code
following a "Dim RecordCount As String" line into a module, but doubt that
was correct.

Thank you again for your support.

Jen
 
P

Peter Jamieson

How to use it depends partly on when you need to insert the result.

For installing and running macros, see Graham Mayor's page at

http://www.gmayor.com/installing_macro.htm

However, you will need a bit more info. to insert the result of the macro.
One way to do it would be to insert a macrobutton (see another of Graham's
pages at

http://www.gmayor.com/Macrobutton.htm

You would need one like

{ MACROBUTTON InsertMailMergeRecordCount [Click here to insert the mailmerge
record count] }

(Use ctrl-F9 to insert the { })

Then add the following to your existing VBA module:

Public Sub InsertMailMergeRecordCount()
Selection.TypeText cstr(MyRecordCount())
End Sub

Peter Jamieson
 
L

Look2TheSky2

Thanks so much for your help.
I've got some reading to do. If I fail to get the results I seek, at least
you have pointed me to a great reference.

Jen
 

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