Maximum number of records in merge

S

stevewy

Can anyone tell me the property that will let me know the number of
records that there are in a particular mail merge, via VBA? On the
Mailmerge toolbar there is an ABC button, which will give you a
preview of the merge. There are also "first record" and "last record"
arrows to take you to opposite ends of your data source respectively.

If I wanted to do something for each record in the merge, a "FOR EACH
record IN merge" kind of thing, which property do I use to access the
number of records in this merge? There must be one somewhere, but I
cannot find it...


Steve Wylie
 
D

Doug Robbins - Word MVP

The VBA command ActiveDocument.MailMerge.DataSource.RecordCount returns the
number of records in the data source.

For the sort of thing that you are talking about however, you more likely
should be using Mail Merge Events.

What exactly is it that you want to do?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
P

Peter Jamieson

If I wanted to do something for each record in the merge, a "FOR EACH
record IN merge" kind of thing, which property do I use to access the
number of records in this merge? There must be one somewhere, but I
cannot find it...

The thing is that because data sources do not necessarily report their
record count accurately using a record count will not necessarily work
anyway.

As Doug says, you can use MailMerge events. If your merge consumes exactly 1
data source record for each output you can also do "1 merge per record"
using a macro such as the folowing (it's not hard to modify it to print) -
the weird little loop should deal with the problem I mention, but you should
test it with your data source. The reason why it's difficult to adapt this
to cope with consumption of varying numbers of records per merge is that
(AFAIK) none of the VBA properties tell you what the next record to merge
is.

Sub ProduceOneDocPerSourceRec()

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.

Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim strOutputDocumentName As String
Dim TerminateMerge As Boolean

Set objMerge = ActiveDocument.MailMerge
With objMerge

' If no data source has been defined, do it here using OpenDataSource.
' But if it is already defined in the document, you should not need to
define it here.

' .OpenDataSource _
' Name:="whatever"

intSourceRecord = 1
TerminateMerge = False

Do Until TerminateMerge
.DataSource.ActiveRecord = intSourceRecord

' if we have gone past the end (and possibly, if there are no records)
' then the Activerecord will not be what we have just tried to set it to

If .DataSource.ActiveRecord <> intSourceRecord Then
TerminateMerge = True
' the record exists
Else

' while we are looking at the correct activerecord,
' create the document path name
' the field names must be identical to the ones Word sees,
' i.e. case is significant here
' e.g. - you will need to change this -
strOutputDocumentName = _
"c:\mydoc\cust" & _
.DataSource.DataFields("CustomerID").Value & ".doc"

.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToNewDocument
.Execute

' The Activedocument is always the output document
' Add any parameters you need to these calls
ActiveDocument.SaveAs strOutputDocumentName
ActiveDocument.Close
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub
 
S

stevewy

Sorry it's taken a while to respond! Thank you both for your
answers.

I need a macro called NspPrint to play itself for each record in the
merge. I have a macro already, but I have to manually alter it each
time for the amount of records I know to be in the merge:

Sub PrintMultipleNSPs()
For a = 1 To 20
Application.Run MacroName:="nspprint"
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Next
End Sub

So am I right, then, in thinking that the macro below would work? I
don't really want to try it out for real in case it starts printing
tonnes of stuff and I can't stop it!

Sub PrintMultipleNSPs2()
For a = 1 To ActiveDocument.MailMerge.DataSource.RecordCount
Application.Run MacroName:="nspprint"
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Next
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