Compartilhar via


MailMergeDataSource.RecordCount Property

Word Developer Reference

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

Syntax

expression.RecordCount

expression   An expression that returns a MailMergeDataSource object.

Remarks

If Microsoft Office 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 fewer 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 fewer than ten digits it will be removed from the mail merge.

Visual Basic for Applications
  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 fewer 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" & _
                "has fewer than five digits. This record will be" & _
                "removed from the mail merge process."
        End If
        If .ActiveRecord &lt;&gt; .<strong class="bterm">RecordCount</strong> Then
            .ActiveRecord = wdNextRecord
        End If
    Loop Until .ActiveRecord = .RecordCount

ErrorHandler:

End With

End Sub

See Also