當使用者在 [合併列印收件者] 對話方塊中選擇 [驗證] 來執行地址驗證時發生。
語法
運算式。MailMergeDataSourceValidate (文件, 已處理)
expression 代表 Application 物件的變數。
參數
| 名稱 | 必要/選用 | 資料類型 | 描述 |
|---|---|---|---|
| Doc | 必要 | Document | 合併列印主文件。 |
| Handled | 必要 | Boolean | True 是表示 隨附的驗證程式碼會針對執行合併列印資料來源。 False 會取消資料來源驗證。 |
註解
如果您的電腦上未安裝地址驗證軟體,請使用 MailMergeDataSourceValidate 事件建立簡單的篩選常式,例如迴圈記錄以檢查郵遞區號,並移除任何非美國的記錄。非美國使用者可以修改下列程式碼範例,並使用 Microsoft Visual Basic 命令搜尋文字或特殊字元,篩選出所有美國郵遞區號。
若要存取 Application 物件事件,請在程式碼模組的 [一般宣告] 區段中宣告 Application 物件變數,然後將變數設定為等於您要存取事件的 Application 物件。
如需將事件與 Microsoft Publisher 應用程式 物件搭配使用的資訊,請參閱搭配 使用事件與應用程式物件。
範例
此範例會驗證附加資料來源中的五位數郵遞區號。 如果郵遞區號長度少於五位數,則記錄會從合併列印程序中排除。 此範例假設郵遞區號為 U.S. ZIP 代碼。 您可以修改此範例來搜尋郵遞區號後附加了四位數定位代碼的郵遞區號,然後排除不包含定位代碼的所有記錄。
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
若要發生此事件,必須將下一行程式碼放在模組的 General Declarations 區段中,並執行下列初始化程式。
Private WithEvents MailMergeApp As Application
Sub InitializeMailMergeApp()
Set MailMergeApp = Publisher.Application
End Sub
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。