MailMergeBeforeMerge Event

Microsoft Word Visual Basic

Show All

MailMergeBeforeMerge Event

       

Occurs when a merge is executed before any records merge.

Private Sub object_MailMergeBeforeMerge(ByVal Doc As Document, ByVal StartRecord As Long, ByVal EndRecord As Long, Cancel As Boolean)

object  An object of type Application declared with events in a class module. For information about using events with the Application object, see Using Events with the Application Object.

Doc   The mail merge main document.

StartRecord   The first record in the data source to include in the mail merge.

EndRecord   The last record in the data source to include in the mail merge.

Cancel   True stops the mail merge process before it starts.

Example

This example displays a message before the mail merge process begins, asking the user if they want to continue. If the user clicks No, the merge process is cancelled. This example assumes that you have declared an application variable called MailMergeApp in your general declarations and have set the variable equal to the Word Application object.

Private Sub MailMergeApp_MailMergeBeforeMerge(ByVal Doc As Document, _
    ByVal StartRecord As Long, ByVal EndRecord As Long, _
    Cancel As Boolean)

    Dim intVBAnswer As Integer

    'Request whether the user wants to continue with the merge
    intVBAnswer = MsgBox("Mail Merge for " & _
        Doc.Name & " is now starting.  " & _
        "Do you want to continue?", vbYesNo, "MailMergeBeforeMerge Event")

    'If users response to question is No, cancel the merge process
    'and deliver a message to the user stating the merge is cancelled
    If intVBAnswer = vbNo Then
        Cancel = True
        MsgBox "You have cancelled mail merge for " & _
            Doc.Name & "."
    End If

End Sub