Application.MailMergeDataSourceValidate 事件 (Publisher)
当用户通过在“邮件合并收件人”对话框中选择“验证”来执行地址验证时发生。
语法
表达式。MailMergeDataSourceValidate (Doc, 已处理)
expression:表示 Application 对象的变量。
参数
名称 | 必需/可选 | 数据类型 | 说明 |
---|---|---|---|
Doc | 必需 | Document | 邮件合并主文档。 |
Handled | 必需 | Boolean | 为 邮件合并数据源运行随附的验证代码。 False 取消数据源验证。 |
备注
如果计算机上未安装地址验证软件,请使用 MailMergeDataSourceValidate 事件创建简单的筛选例程,例如循环访问记录以检查邮政编码,并删除任何非美国的。非美国用户可以通过修改以下代码示例并使用 Microsoft Visual Basic 命令搜索文本或特殊字符来筛选出所有美国邮政编码。
若要访问 Application 对象事件,请在代码模块的“常规声明”部分中声明 Application 对象变量 ,然后将该变量设置为要访问其事件的 Application 对象。
有关对 Microsoft Publisher Application 对象使用事件的信息,请参阅 对 Application 对象使用事件。
示例
本示例验证附加数据源中的邮政编码是否为五位数字。 如果邮政编码的长度少于五位,该记录将从邮件合并过程中排除。 此示例假定邮政编码为美国邮政编码。 可以修改此示例以搜索在邮政编码中追加了四位数定位符代码的邮政编码,然后排除不包含定位符代码的所有记录。
Private Sub MailMergeApp_MailMergeDataSourceValidate( _
ByVal Doc As Document, _
Handled As Boolean)
Dim intCount As Integer
Handled = True
On Error Resume Next
With ActiveDocument.MailMerge.DataSource
'Set the active record equal to the first included record in the
'data source
.ActiveRecord = 1
Do
intCount = intCount + 1
'Set the condition that field six must be greater than or
'equal to five
If Len(.DataFields.Item(6).Value) < 5 Then
'Exclude the record if field six is shorter 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 has " _
& "fewer 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 = .ActiveRecord + 1
'End the loop when the counter variable
'equals the number of records in the data source
Loop Until intCount = .RecordCount
End With
End Sub
要使该事件发生,必须将以下代码行置于模块中的通用声明部分,并运行以下的初始化例程。
Private WithEvents MailMergeApp As Application
Sub InitializeMailMergeApp()
Set MailMergeApp = Publisher.Application
End Sub
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈