首页 word技巧 word之VBA RecordCount属性使用示例

word之VBA RecordCount属性使用示例

发布:2024-02-26 18:21:08 阅读:98

word中的vba里面的RecordCount属性,其功能是返回一个 Long对象,该对象代表数据源中记录的数字为只读。

语法如下:

expression.RecordCount

参数说明

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

注意:

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

以下内容是有关RecordCount的代码示例

On Error GoTo ErrorHandler

With ActiveDocument.MailMerge.DataSource

.ActiveRecord = wdFirstRecord

Do

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

上面的代码循环遍历数据源中的记录并验证邮政编码域(在本示例中是第六个域)是否少于五位,如果少于五位,则从邮件合并中删除该记录。

如果要确保将定位代码添加到邮政编码中,可以将长度值从 5 改为 10。从而,如果邮政编码少于 10 位,就将其从邮件合并中删除。

上一篇:Word删除空行图文教程 下一篇:如何使用Word2010中"不压缩文件中的图像"提高图片质量
相关文章 查看更多