Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2005

Tip: Looking for answers? Try searching our database.

MailmergeAfterRecordMerge Event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Doug Robbins - 02 Jul 2005 23:04 GMT
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
Helmut Weber - 04 Jul 2005 13:42 GMT
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"
Doug Robbins - 04 Jul 2005 17:49 GMT
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

Signature

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

> Hi Doug,
>
[quoted text clipped - 13 lines]
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.