• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Linking to a Text file with mailMerge


I'm creating a MailMerge project.  I'd like to link to a text file as my data source.

Public Sub CreateMailMergeDataFile()
Dim wrdDataDoc As Word.Document
Dim iCount As Integer

' Create a data source at C:\DataDoc.doc containing the field data
wrdDoc.MailMerge.CreateDataSource Name:="C:\DataDoc.doc", _
        HeaderRecord:="FirstName, LastName, Address, CityStateZip"

' Open the file to insert data
Set wrdDataDoc = wrdApp.Documents.Open("C:\DataDoc.doc")

' Save and close the file
'wrdDataDoc.Close False

End Sub

Is there any way to link to an existing .txt file to retrieve the data required?
1 Solution
To create a catalog of data you could use this:

sub MergeToCatalog()

Dim objWord As Word.Application
Dim wrdDoc As Word.Document
Dim wrdTable As Word.Table
Dim wrdRow As Word.Row
Dim wrdRange As Word.Range
Dim wrdMergeField As Word.MailMergeField
dim strOutDoc as string

strOutDoc = "c:\temp\data.txt"

    Set objWord = New Word.Application
    ' new file
        Set wrdDoc = objWord.Documents.Add
        '  make document mail merge
        With wrdDoc.MailMerge
            .MainDocumentType = wdCatalog
            .OpenDataSource Name:=strOutDoc, LinkToSource:=True, AddToRecentFiles:=False
        End With

        Set wrdRange = wrdDoc.Content
        wrdRange.Collapse wdCollapseEnd
        ' create table to display info
        Set wrdTable = wrdDoc.Tables.Add(wrdRange, 1, intNumFields - 1)
        wrdTable.Rows.SpaceBetweenColumns = 0
        wrdTable.Borders.Enable = False
'Put in fields

        bolFirst = True
        For lngX = 1 To UBound(arrtypOutput)
            Set wrdRange = wrdTable.Rows.First.Cells(lngX).Range
            Set wrdMergeField = wrdDoc.MailMerge.Fields.Add(Range:=wrdRange, _
        ' merge to new document
       strPoint = "Create Distribution list - Do merge to document"
        With wrdDoc.MailMerge
            .Destination = wdSendToNewDocument
        End With
        wrdDoc.Close wdDoNotSaveChanges
        Set wrdDoc = objWord.Documents(1)
objword.visible = true

end sub


Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now