Solved

Linking to a Text file with mailMerge

Posted on 2001-08-14
1
193 Views
Last Modified: 2009-12-16
All:

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"

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


' Save and close the file
'wrdDataDoc.Save
'wrdDataDoc.Close False

End Sub



Is there any way to link to an existing .txt file to retrieve the data required?
0
Comment
Question by:damunj
1 Comment
 
LVL 10

Accepted Solution

by:
RichardCorrie earned 50 total points
ID: 6385014
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, _
              Name:=arrtypOutput(lngX).strMergeName)
        Next
       
        ' merge to new document
       strPoint = "Create Distribution list - Do merge to document"
        With wrdDoc.MailMerge
            .Destination = wdSendToNewDocument
            .Execute
        End With
        wrdDoc.Close wdDoNotSaveChanges
       
        Set wrdDoc = objWord.Documents(1)
objword.visible = true

end sub

Richard
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question