MailmergeAfterRecordMerge Event


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
 
Ad

Advertisements

H

Helmut Weber

Hi Doug,

though I have very limited experience with mail merge,
I sometimes found, that all these three settings are necessary:

..ActiveRecord = whatever
..FirstRecord = whatever
..LastRecord = whatever

At a long shot.


Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Doug Robbins

Hi Helmut,

Cindy came up with this that does the trick

OK, before I tested I made a change: I declared DocResult as module-level
variable and instantiate it during BeforeRecordMerge. And after the merge
ends,
I release it. I don't know whether this had anything to do with the problem
you
were experiencing, or not, the way you were handling it just felt wrong to
me.

In any case, it worked here, first time. The code in my bas module:

Option Explicit

Dim x As New Class1

Sub ActivateEvents()
Set x.app = Word.Application
End Sub

Sub DeactivateEvents()
Set x.app = Nothing
End Sub
============
And in my class module:

Option Explicit

Public WithEvents app As Word.Application
Private DocResult As Word.Document

Private Sub app_MailMergeAfterMerge(ByVal Doc As Document, ByVal DocResult
As
Document)
Set DocResult = Nothing
End Sub

Private Sub app_MailMergeAfterRecordMerge(ByVal Doc As Document)
Dim NewDoc As Document, i As Long ', docResult As Document
With Doc.MailMerge.DataSource
' Set DocResult = ActiveDocument
'located here or before the With Doc.MailMerge.DataSource.
i = DocResult.Sections.Count - 1

Set NewDoc = Documents.Add
NewDoc.Range.FormattedText =
DocResult.Sections(i).Range.FormattedText
NewDoc.SaveAs "C:\test\" & .DataFields(2).Value
NewDoc.Close
Set NewDoc = Nothing
' Set DocResult = Nothing
End With
End Sub

Private Sub app_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As
Boolean)
If DocResult Is Nothing Then
If InStr(1, ActiveDocument.Name, "Letter") <> 0 Then
Set DocResult = ActiveDocument
Else
MsgBox "Active document is: " & ActiveDocument.Name & vbCr &
"Cannot process."
Cancel = True
End If
End If
End Sub


--
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
 
Joined
Sep 27, 2017
Messages
1
Reaction score
0
In the solution above, you propose to put some code into the "bas module:". I understood this to be the subfolder "This Document" under the folder "Microsoft Word Objects" of my mail merge template. I have named the class module "MailMergeToPDF" and have adapted the instantiation of the class object "x" as follows: "Dim x As New MailMergeToPDF". In a first attempt to trap the AfterRecordMerge-event, apparently, the appropriate sub never gains control. What did I do wrong? Greetings, Han Schouten, Margraten, the Netherlands.
 
Ad

Advertisements

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
522
Reaction score
48
I seriously doubt any of the original contributors are following this thread some 12 years later...

More importantly, as the note at the top of this page says:
This forum sub-section is an archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections

For code to output mailmerge records to separate files, see Send Mailmerge Output to Individual Files in the Mailmerge Tips and Tricks thread at:
http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
or:
http://windowssecrets.com/forums/showthread.php/163017-Word-Mailmerge-Tips-amp-Tricks
 

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