Adres mektup etiketleri için adres mektup birleştirme oluşturmak üzere Visual Basic'ten Word'i otomatikleştirme

Özet

Bu makalede, posta etiketleri için adres mektup birleştirme oluşturmak ve yürütmek üzere Microsoft Visual Basic uygulamasından Microsoft Office Word'ün nasıl otomatikleştirilecekleri açıklanır.

Daha Fazla Bilgi

Bu makaledeki örnek kod, veri kaynağı için sekmeyle ayrılmış bir metin dosyası kullanır. Metin dosyasında aşağıdaki alanlar bulunur:

  • Contact_Name
  • Adres
  • Şehir
  • Postal_Code
  • Ülke

Metin dosyası veri kaynağını oluşturmak için herhangi bir metin düzenleyicisini kullanabilirsiniz. Örneğin, Not Defteri'ni kullanabilirsiniz.

Veri kaynağını oluştururken, sekme karakteri kullanarak alanları (sütunları) ve satır başı kullanarak kayıtları (satırları) ayırmayı unutmayın. Aşağıda, metin dosyasının nasıl görünebileceğine ilişkin bir örnek verilmiştir:

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

Not

Sekmeyle sınırlandırılmış metin dosyası yerine başka bir veri kaynağı kullanabilirsiniz. Örneğin, bir Microsoft Access veritabanı kullanabilirsiniz.

Adım adım örnek

  1. Visual Basic'te yeni bir Standart EXE projesi başlatın. Varsayılan olarak, Form1 adlı bir form oluşturulur.
  2. Form1'e CommandButton ekleyin.
  3. Otomatikleştirmek istediğiniz Word sürümü için Microsoft Word Nesne Kitaplığı'nı seçin ve ardından Tamam'a tıklayın.
  4. Aşağıdaki kodu Form1'in kod penceresine kopyalayın.
    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
    
    
    Bu koddaki OpenDataSource yönteminin Ad bağımsız değişkeni, veri kaynağına c:\data.txt olarak başvurur. Veri kaynağının farklı bir yolu veya farklı bir dosya adı varsa, koddaki bu satırı buna göre değiştirin.
  5. Programı çalıştırmak için F5 tuşuna basın ve ardından Command1'e tıklayın. Posta etiketi belgesi, veri kaynağından alınan veriler kullanılarak oluşturulur.

Başvurular

Word'ü otomatikleştirme veya adres mektup birleştirme belgeleri oluşturma hakkında daha fazla bilgi için, Microsoft Bilgi Bankası'ndaki makaleleri görüntülemek üzere aşağıdaki makale numaralarına tıklayın:

220911 Visual C++ ve MFC kullanarak adres mektup birleştirme gerçekleştirmek için Microsoft Word'u otomatikleştirme

212034 Word'de Adres Mektup Birleştirme kullanarak posta etiketleri oluşturma