Example of using the VBA RecordCount property for Word

Source: Internet
Author: User

The RecordCount property in VBA in Word, which functions by returning a long object that represents the number of records in the data source as read-only.

The syntax is as follows:

Expression. RecordCount

Parameter description

expression Required. The expression returns a MailMergeDataSource object.

Attention:

If Microsoft Word cannot determine the number that is recorded in the data source, the RecordCount property returns a value of 1.

The following is a code example for RecordCount

On Error GoTo ErrorHandler

With ActiveDocument.MailMerge.DataSource

. ActiveRecord = wdFirstRecord

Todo

If Len (. DataFields (6). Value) < 5 Then

. Included = False

. InvalidAddress = True

. InvalidComments = "The ZIP code for this record" & _

' is less than five digits. This is ' & _

"Removed from the mail merge process."

End If

If. ActiveRecord <>. RecordCount Then

. ActiveRecord = wdNextRecord

End If

Loop Until. ActiveRecord =. RecordCount

ErrorHandler:

End With

The above code loops through the records in the data source and verifies that the postal Code field (in this example, the sixth field) is less than five digits and, if less than five bits, deletes the record from the mail merge.

If you want to make sure that the location code is added to your zip code, you can change the length value from 5 to 10. Thus, if the postal code is less than 10 digits, it is removed from the mail merge.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.