当用户通过单击“邮件合并收件人”对话框中的“验证有效性”执行地址验证时,该事件发生。
Private Sub object_MailMergeDataSourceValidate(ByVal Doc As Document, Handled As Boolean)
object 在类模块事件中声明的 Application 类型对象。有关使用 Application 对象事件的详细信息,请参阅使用 Application 对象事件。
Doc 邮件合并主文档。
Handled 如果该参数值为 True,则对邮件合并数据源运行伴随的验证代码。如果该参数值为 False,则取消数据源验证。
如果未在计算机上安装地址验证软件,MailMergeDataSourceValidate 事件允许您创建简单的筛选例程,例如循环查找记录以检查邮政编码,并删除任何非美国的邮政编码。美国以外的用户可以通过更改以下代码示例和使用 Microsoft Visual Basic 命令搜索文本或特殊字符,筛选出所有的美国邮政编码。
该示例显示一条消息,询问是否验证数据源中的地址。该示例假定邮政编码为美国邮政编码,并且在一般声明中声明了一个名为 MailMergeApp 的应用程序变量,并将 Word Application 对象赋给该变量。(您可以更改该示例,以筛选国际邮政编码或其他具有附加的 4 位数字定位代码的邮政编码)
Private Sub MailMergeApp_MailMergeDataSourceValidate(ByVal Doc As Document, _
Handled As Boolean)
Dim intCount As Integer
Handled = True
On Error Resume Next
With Doc.MailMerge.DataSource
'Set the active record equal to the first record in the data source
.ActiveRecord = wdFirstRecord
Do
intCount = intCount + 1
'Set the condition that field six must be greater than or
'equal to five
If Len(.DataFields(6).Value) < 5 Then
'Exclude the record if field six is less than five digits
.Included = False
'Mark the record as containing an invalid address field
.InvalidAddress = True
'Specify the comment attached to the record explaining
'why the record was excluded from the mail merge
.InvalidComments = "The zip code for this record is " _
& "less than five digits. It will be removed " _
& "from the mail merge process."
End If
'Move the record to the next record in the data source
.ActiveRecord = wdNextRecord
'End the loop when the counter variable
'equals the number of records in the data source
Loop Until intCount = .RecordCount
End With
End Sub