Application.MailMergeDataSourceValidate イベント (Publisher)

[差し込み印刷の受信者] ダイアログ ボックスで [検証] を選択して、ユーザーがアドレス検証を実行したときに発生します。

構文

MailMergeDataSourceValidate (DocHandled)

expressionApplication オブジェクトを 表す変数。

パラメーター

名前 必須 / オプション データ型 説明
Doc 必須 Document 差し込み印刷のメイン文書を指定します。
Handled 必須 ブール型 (Boolean) True 差し込み印刷データ ソースに対して、付属の確認コードを実行します。 False は、データ ソースの確認をキャンセルします。

注釈

コンピューターにアドレス確認ソフトウェアがインストールされていない場合は、 MailMergeDataSourceValidate イベントを使用して、レコードをループして郵便番号を確認したり、米国以外のものを削除したりなどの単純なフィルター処理ルーチンを作成します。米国以外のユーザーは、次のコード サンプルを変更し、Microsoft Visual Basic コマンドを使用してテキストまたは特殊文字を検索することで、すべての米国の郵便番号を除外できます。

Application オブジェクト イベントにアクセスするには、コード モジュールの [全般宣言] セクションで Application オブジェクト変数を宣言し、その変数をイベントにアクセスする Application オブジェクトと等しく設定します。

Microsoft Publisher Application オブジェクトでイベントを使用する方法については、「 Application オブジェクト でのイベントの使用」を参照してください。

次の使用例は、添付したデータ ソースの郵便番号が 5 桁かどうかを確認します。 郵便番号が 5 桁未満の場合、そのレコードは差し込み印刷処理から除外されます。 この例では、郵便番号が米国の郵便番号であることを前提としています。 この例を変更して、郵便番号に 4 桁のロケーター コードが付加されている郵便番号を検索し、ロケーター コードを含まないすべてのレコードを除外できます。

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 のサポートおよびフィードバックを参照してください。