RecordCount Property

Microsoft Publisher Visual Basic

expression.RecordCount

expression    Required. An expression that returns a MailMergeDataSource 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.

Sub Validate

    Dim intCount As Integer
    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 digits in length
            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