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