MailMergeDataSourceValidate Event

Microsoft Publisher Visual Basic

Private Sub object_MailMergeDataSourceValidate(ByVal Doc As Document, Handled As Boolean)

object    A variable which references an object of type Application declared with events in a class module.

Doc Required. The mail merge main document.

Handled Optional. True runs the accompanying validation code against the mail merge data source. False cancels the data source validation.

Remarks

If you don't have address verification software installed on your computer, use the MailMergeDataSourceValidate event to create simple filtering routines, such as looping through records to check the postal codes and remove any that are non-U.S. Non-U.S. users can filter out all U.S. postal codes by modifying the code sample below and using Visual Basic commands to search for text or special characters.

To access the Application object events, declare an Application object variable in the General Declarations section of a code module. Then set the variable equal to the Application object for which you want to access events. For information about using events with the Publisher Application object, see Using Events with the Application Object.

Example

This example validates ZIP codes in the attached data source for five digits. If the length of the ZIP code is less than five, the record is excluded from the mail merge process. This example assumes the postal codes are U.S. ZIP codes. You could modify this example to search for ZIP codes that have a 4-digit locator code appended to the ZIP code, and then exclude all records that don't contain the locator code.

Private Sub MailMergeApp_MailMergeDataSourceValidate( _
        ByVal Doc As Document, _
        Handled As Boolean)

    Dim intCount As Integer

    Handled = True

    On Error Resume Next

    With ActiveDocument.MailMerge.DataSource

        'Set the active record equal to the first included record in the
        'data source
        .ActiveRecord = 1
        Do
            intCount = intCount + 1

            'Set the condition that field six must be greater than or
            'equal to five
            If Len(.DataFields.Item(6).Value) < 5 Then

                'Exclude the record if field six is less than five digits
                .Included = False

                'Mark the record as containing an invalid address field
                .InvalidAddress = True

                'Specify the comment attached to the record explaining
                'why the record was excluded from the mail merge
                .InvalidComments = "The ZIP code for this record is " _
                    & "less than five digits. It will be removed " _
                    & "from the mail merge process."

            End If

            'Move the record to the next record in the data source
            .ActiveRecord = .ActiveRecord + 1

        'End the loop when the counter variable
        'equals the number of records in the data source
        Loop Until intCount = .RecordCount
    End With

End Sub
		

For this event to occur, you must place the following line of code in the General Declarations section of your module and run the following initialization routine.

Private WithEvents MailMergeApp As Application

Sub InitializeMailMergeApp()
    Set MailMergeApp = Publisher.Application
End Sub