D
Doug Robbins
Hi All,
I am trying to create a utility to merge each record in a mailmerge to a
separate document, saved with a name from one of the fields in the
datasource, those documents being in addition to the document that is
created by the mailmerge. For that purpose, I am using the following code:
Private Sub oApp_MailMergeAfterRecordMerge(ByVal Doc As Document)
Dim NewDoc As Document, i As Long, DocResult As Document
With Doc.MailMerge.DataSource
Set DocResult = ActiveDocument 'The document created by the
mailmerge; It doesn't make any difference if this is
'located here or before the With Doc.MailMerge.DataSource.
i = DocResult.Sections.Count - 1 'After each record is merged, a
continuous section break is inserted so the
'document created by the mailmerge contains one more section than
the number of records merged.
Set NewDoc = Documents.Add
NewDoc.Range.FormattedText =
DocResult.Sections(i).Range.FormattedText
NewDoc.SaveAs .DataFields(2).Value
NewDoc.Close
Set NewDoc = Nothing 'I have tried with and without setting these
to nothing.
Set DocResult = Nothing
End With
End Sub
It works partially, creating the desired separate documents, except that, no
matter how many records there are in the datasource, the merge stops after
the second record with the document created by the mailmerge containing the
data for the first two records only and only the first two of the separate
documents being created.
If I run the following code from the help file, the new document created by
the merge contains the data for all records in the data source, and the
MsgBox displays the information for all records as well.
Private Sub oApp_MailMergeAfterRecordMerge(ByVal Doc As Document)
Dim NewDoc As Document, i As Long, DocResult As Document
With Doc.MailMerge.DataSource
MsgBox .DataFields(1).Value & " " & _
.DataFields(2).Value & " is finished merging."
End With
End Sub
Does anyone know what I am missing?
Doug Robbins - Word MVP
I am trying to create a utility to merge each record in a mailmerge to a
separate document, saved with a name from one of the fields in the
datasource, those documents being in addition to the document that is
created by the mailmerge. For that purpose, I am using the following code:
Private Sub oApp_MailMergeAfterRecordMerge(ByVal Doc As Document)
Dim NewDoc As Document, i As Long, DocResult As Document
With Doc.MailMerge.DataSource
Set DocResult = ActiveDocument 'The document created by the
mailmerge; It doesn't make any difference if this is
'located here or before the With Doc.MailMerge.DataSource.
i = DocResult.Sections.Count - 1 'After each record is merged, a
continuous section break is inserted so the
'document created by the mailmerge contains one more section than
the number of records merged.
Set NewDoc = Documents.Add
NewDoc.Range.FormattedText =
DocResult.Sections(i).Range.FormattedText
NewDoc.SaveAs .DataFields(2).Value
NewDoc.Close
Set NewDoc = Nothing 'I have tried with and without setting these
to nothing.
Set DocResult = Nothing
End With
End Sub
It works partially, creating the desired separate documents, except that, no
matter how many records there are in the datasource, the merge stops after
the second record with the document created by the mailmerge containing the
data for the first two records only and only the first two of the separate
documents being created.
If I run the following code from the help file, the new document created by
the merge contains the data for all records in the data source, and the
MsgBox displays the information for all records as well.
Private Sub oApp_MailMergeAfterRecordMerge(ByVal Doc As Document)
Dim NewDoc As Document, i As Long, DocResult As Document
With Doc.MailMerge.DataSource
MsgBox .DataFields(1).Value & " " & _
.DataFields(2).Value & " is finished merging."
End With
End Sub
Does anyone know what I am missing?
Doug Robbins - Word MVP