Automatisieren von Word aus Visual Basic zum Erstellen eines Seriendrucks für Adressetiketten
Zusammenfassung
In diesem Artikel wird beschrieben, wie Sie Microsoft Office Word aus einer Microsoft Visual Basic-Anwendung automatisieren, um einen Seriendruck für Adressetiketten zu erstellen und auszuführen.
Weitere Informationen
Der Beispielcode in diesem Artikel verwendet eine durch Tabstopps getrennte Textdatei für die Datenquelle. Die Textdatei weist die folgenden Felder auf:
- Contact_Name
- Adresse
- Stadt
- Postal_Code
- Land
Sie können einen beliebigen Text-Editor verwenden, um die Datenquelle der Textdatei zu erstellen. Sie können z. B. Editor verwenden.
Denken Sie beim Erstellen der Datenquelle daran, die Felder (Spalten) mithilfe eines Tabstoppzeichens zu trennen und die Datensätze (Zeilen) mithilfe eines Wagenrücklaufs zu trennen. Es folgt ein Beispiel für die Darstellung der Textdatei:
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
Hinweis
Sie können eine beliebige andere Datenquelle anstelle der durch Tabstopps getrennten Textdatei verwenden. Sie können z. B. eine Microsoft Access-Datenbank verwenden.
Schritt-für-Schritt-Beispiel
- Starten Sie ein neues Standard EXE-Projekt in Visual Basic. Standardmäßig wird ein Formular mit dem Namen "Form1" erstellt.
- Fügen Sie form1 ein CommandButton-Objekt hinzu.
- Wählen Sie die Microsoft Word-Objektbibliothek für die Version von Word aus, die Sie automatisieren möchten, und klicken Sie dann auf "OK".
- Kopieren Sie den folgenden Code in das Codefenster von Form1.
Das Name-Argument für die OpenDataSource-Methode in diesem Code verweist auf die Datenquelle als c:\data.txt. Wenn die Datenquelle einen anderen Pfad oder einen anderen Dateinamen hat, ändern Sie diese Zeile im Code entsprechend.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
- Drücken Sie F5, um das Programm auszuführen, und klicken Sie dann auf Befehl1. Ein Adressetikettdokument wird mithilfe von Daten erstellt, die aus der Datenquelle stammen.
References
Weitere Informationen zum Automatisieren von Word oder zum Erstellen von Seriendruckdokumenten finden Sie in den folgenden Artikelnummern, um die Artikel in der Microsoft Knowledge Base anzuzeigen:
220911 Automatisieren von Microsoft Word zum Ausführen eines Seriendrucks mit Visual C++ und MFC
212034 Erstellen von Adressetiketten mithilfe des Seriendrucks in Word