RecordCount Property

Microsoft Word Visual Basic

RecordCount Property

       

Returns a Long that represents the number of records in the data source.  Read-only.

expression.RecordCount

expression   Required. An expression that returns a MailMergeDataSource object.

Remarks

If Microsoft Word cannot determine the number of records in a data source, the RecordCount property will return a value of -1.

Example

This example loops through the records in the data source and verifies that the postal code field (field six in this example) is not less than five digits. If it is, it removes the record from the mail merge. If you want to make sure that the locator code is added to the postal code, you can change the length value from 5 to 10. Therefore, if a postal code is less than ten digits it will be removed from the mail merge.

Sub ExcludeRecords()

    On Error GoTo ErrorHandler

    With ActiveDocument.MailMerge.DataSource
        .ActiveRecord = wdFirstRecord
        Do

            'Counts the number of digits in the postal code field and if
            'it is less than 5, the record is excluded from the mail merge,
            'marked as having an invalid address, and given a comment
            'describing why the postal code was removed
            If Len(.DataFields(6).Value) < 5 Then
                .Included = False
                .InvalidAddress = True
                .InvalidComments = "The zip code for this record" & _
                    "is less than five digits. This record is" & _
                    "removed from the mail merge process."
            End If
            If .ActiveRecord <> .RecordCount Then
                .ActiveRecord = wdNextRecord
            End If
        Loop Until .ActiveRecord = .RecordCount
ErrorHandler:

    End With

End Sub