萬盛學電腦網

 萬盛學電腦網 >> WORD教程 >> WORD使用技巧 >> word之VBA RecordCount屬性使用示例

word之VBA RecordCount屬性使用示例

  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 位,就將其從郵件合並中刪除。

copyright © 萬盛學電腦網 all rights reserved