共用方式為


Application.MailMergeDataSourceValidate 事件 (Publisher)

發生于使用者在 [合併列印收件者] 對話方塊中選擇 [驗證] 來執行位址驗證時。

語法

運算式MailMergeDataSourceValidate (DocHandled)

expression 代表 Application 物件的變數。

參數

名稱 必要/選用 資料類型 描述
Doc 必要 Document 合併列印主文件。
Handled 必要 Boolean True 是表示 隨附的驗證程式碼會針對執行合併列印資料來源。 False 會取消資料來源驗證。

註解

如果您的電腦上未安裝位址驗證軟體,請使用 MailMergeDataSourceValidate 事件來建立簡單的篩選常式,例如迴圈查看記錄以檢查郵遞區號,以及移除任何非美國非美國使用者可以修改下列程式碼範例,並使用 Microsoft Visual Basic 命令來搜尋文字或特殊字元,以篩選掉所有美國郵遞區號。

若要存取 Application 物件事件,請在程式碼模組 的 [一 般宣告] 區段中宣告 Application 物件變數,然後將變數設定為等於您要存取事件的 Application 物件。

如需搭配 Microsoft Publisher Application 物件使用事件的相關資訊,請參閱 搭配 Application 物件使用事件

範例

此範例會驗證附加資料來源中五位數的 ZIP 代碼。 如果 ZIP 程式碼的長度少於五位數,則記錄會從合併列印程式中排除。 此範例假設郵遞區號為美國郵遞區號。 您可以修改此範例來搜尋將四位數定位器程式碼附加至 ZIP 程式碼的 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 支援與意見反應