RecordCount 属性

       

返回一个 Long 对象,该对象代表数据源中记录的数字。只读。

expression.RecordCount

expression   必需。该表达式返回一个 MailMergeDataSource 对象。

说明

如果 Microsoft Word 不能确定数据源中记录的数字,RecordCount 属性会返回值 -1。

示例

本示例循环遍历数据源中的记录并验证邮政编码域(在本示例中是第六个域)是否少于五位,如果少于五位,则从邮件合并中删除该记录。如果要确保将定位代码添加到邮政编码中,可以将长度值从 5 改为 10。从而,如果邮政编码少于 10 位,就将其从邮件合并中删除。

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