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.