Brett Barabash
BBarabash at TappeConstruction.com
Mon Dec 1 13:26:28 CST 2003
Mark,
First and foremost, use a text file as your mail merge data source. Having
gone down this painful path, I can tell you that trying to link to Access
data from is no fun!
I normally use non-standard delimiters for my fields and records. By
default, Word uses Tabs to delimit fields and Carriage Returns for
delimiting records. By using something else (in my case tildes and carats),
I can merge in lists, paragraph blocks, or whatever else I want (as long as
it doesn't contain ~ or ^).
That being said, here is some code that I posted a while back, that will
generate a mail merge text file based off of any Table or QueryDef:
Public Sub CreateMergeDataSource(ByVal strTblQry As String, ByVal
strFilePath As String)
Const cFieldDelim = "~"
Const cRecordDelim = "^"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim lngFileNo As Long
Dim strBuffer As String
Set db = CurrentDb()
Set rs = db.OpenRecordset(strTblQry, dbOpenSnapshot)
'Initialize field name list (first line of data source)
For Each fld In rs.Fields
Select Case fld.Type
Case dbGUID, dbLongBinary, dbMemo
'Do not include these types of fields
Case Else
strBuffer = strBuffer & Chr$(34) & fld.Name & Chr$(34) &
cFieldDelim
End Select
Next fld
strBuffer = strBuffer & cRecordDelim
'Loop through all records in source table/query
Do Until rs.EOF
For Each fld In rs.Fields
Select Case fld.Type
Case dbGUID, dbLongBinary, dbMemo
'Do not include these types of fields
Case Else
strBuffer = strBuffer & Chr$(34) & fld.Value & Chr$(34) &
cFieldDelim
End Select
Next fld
strBuffer = strBuffer & cRecordDelim
rs.MoveNext
Loop
rs.Close
'Generate data source text file
lngFileNo = FreeFile
Open strFilePath For Output As #lngFileNo
Print #lngFileNo, strBuffer
Close #lngFileNo
Set rs = Nothing
Set db = Nothing
End Sub
I don't have the actual automation code handy, but it should be something
like this (I haven't tested the code below, possibly someone else can share
their secret recipe):
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Set wdApp = New Word.Application
Set wdDoc = wdApp.Open "c:\mydir\mytemplate.doc"
With wdDoc.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:=c:\mydir\mysource.txt, Format:=wdOpenFormatText
.Destination = wdSendToNewDocument
.Execute
End With
wdApp.UserControl = True
wdApp.Visible = True
Set wdApp = Nothing
Set wdDoc = Nothing
-----Original Message-----
From: Porter, Mark [mailto:MPorter at acsalaska.com]
Sent: Monday, December 01, 2003 12:22 PM
To: 'Access Developers discussion and problem solving'
Subject: RE: [AccessD] Word templates from Access
Does anyone have any code snippets that shows how to send an Access querydef
to a Word mail merge template?
I have a client with quite a few form letters they would like to send data
too.
Mark
--------------------------------------------------------------------------------------------------------------------
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom
they are addressed.
If you have received this email in error please notify the
originator of the message. This footer also confirms that this
email message has been scanned for the presence of computer viruses.
Any views expressed in this message are those of the individual
sender, except where the sender specifies and with authority,
states them to be the views of Tappe Construction Co.
Scanning of this message and addition of this footer is performed
by SurfControl E-mail Filter software in conjunction with
virus detection software.