Solved

Linking to a Text file with mailMerge

Posted on 2001-08-14
1
192 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Acccess - Update Field in Table when report is printed 6 43
using web browser with BING 40 110
Macro Excel - Multiple If conditions 2 63
vbModal 12 31
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now