Visual Basic から Word を自動化して宛名ラベルの差し込み印刷を作成する方法

概要

この記事では、Microsoft Visual Basic アプリケーションから Microsoft Office Word を自動化して、宛名ラベルの差し込み印刷を作成して実行する方法について説明します。

詳細情報

この記事のサンプル コードでは、データ ソースにタブ区切りのテキスト ファイルを使用します。 テキスト ファイルには、次のフィールドがあります。

  • Contact_Name
  • アドレス
  • 市区町村
  • Postal_Code

任意のテキスト エディターを使用して、テキスト ファイル データ ソースを作成できます。 たとえば、メモ帳を使用できます。

データ ソースを作成するときは、タブ文字を使用してフィールド (列) を区切り、キャリッジ リターンを使用してレコード (行) を区切ります。 テキスト ファイルの表示方法の例を次に示します。

Contact_NameAddress City Postal_Code Country
Maria AndersObere Str. 57 Berlin 12209 Germany 
Thomas Hardy120 Hanover Sq. London WA1 1DP UK
Hanna MoosForsterstr. 57 Mannheim 68306 Germany
Laurence Lebihan 12, rue des Bouchers Marseille 13008 France

注:

タブ区切りのテキスト ファイルの代わりに、他の任意のデータ ソースを使用できます。 たとえば、Microsoft Access データベースを使用できます。

ステップ バイ ステップの例

  1. Visual Basic で新しい Standard EXE プロジェクトを開始します。 既定では、Form1 という名前のフォームが作成されます。
  2. Form1 に CommandButton を追加します。
  3. 自動化する Word のバージョンの Microsoft Word オブジェクト ライブラリを選択し、[OK] をクリックします。
  4. 次のコードを Form1 のコード ウィンドウにコピーします。
    Private Sub Command1_Click()
    
    Dim oApp As Word.Application
        Dim oDoc As Word.Document
    
    'Start a new document in Word
        Set oApp = CreateObject("Word.Application")
        Set oDoc = oApp.Documents.Add
    
    With oDoc.MailMerge
    
    'Insert the mail merge fields temporarily so that
            'you can use the range that contains the merge fields as a layout
            'for your labels -- to use this as a layout, you can add it
            'as an AutoText entry.
            With .Fields
                .Add oApp.Selection.Range, "Contact_Name"
                oApp.Selection.TypeParagraph
                .Add oApp.Selection.Range, "Address"
                oApp.Selection.TypeParagraph
                .Add oApp.Selection.Range, "City"
                oApp.Selection.TypeText "  "
                .Add oApp.Selection.Range, "Postal_Code"
                oApp.Selection.TypeText " -- "
                .Add oApp.Selection.Range, "Country"
            End With
            Dim oAutoText As Word.AutoTextEntry
            Set oAutoText = oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", oDoc.Content)
            oDoc.Content.Delete 'Merge fields in document no longer needed now
                                'that the AutoText entry for the label layout
                                'has been added so delete it.
    
    'Set up the mail merge type as mailing labels and use
            'a tab-delimited text file as the data source.
            .MainDocumentType = wdMailingLabels 
            .OpenDataSource Name:="C:\data.txt" 'Specify the data source here
    
    'Create the new document for the labels using the AutoText entry
            'you added -- 5160 is the label number to use for this sample.
            'You can specify the label number you want to use for the output
            'in the Name argument.
            oApp.MailingLabel.CreateNewDocument Name:="5160", Address:="", _
                AutoText:="MyLabelLayout", LaserTray:=wdPrinterManualFeed
    
    'Execute the mail merge to generate the labels.
            .Destination = wdSendToNewDocument
            .Execute
    
    'Delete the AutoText entry you added
            oAutoText.Delete
    
    End With
    
    'Close the original document and make Word visible so that
    
    'the mail merge results are displayed
        oDoc.Close False
        oApp.Visible = True
    
    'Prevent save to Normal template when user exits Word
        oApp.NormalTemplate.Saved = True
    
    End Sub
    
    
    このコードの OpenDataSource メソッドの Name 引数は、データ ソースをc:\data.txtとして参照します。 データ ソースに別のパスまたは別のファイル名がある場合は、それに応じてコード内のこの行を変更します。
  5. F5 キーを押してプログラムを実行し、Command1 をクリックします。 メーリング ラベル ドキュメントは、データ ソースから取得されたデータを使用して作成されます。

関連情報

Word を自動化する方法や差し込み印刷ドキュメントを作成する方法の詳細については、次の記事番号をクリックして、Microsoft サポート技術情報の記事を表示してください。

220911 Visual C++ と MFC を使用して Microsoft Word を自動化して差し込み印刷を実行する方法

212034 Word で差し込み印刷を使用して宛名ラベルを作成する方法