Article ID: 170988
Article Last Modified on 1/20/2007
Argument Value
-------- ------------------------------------------------------
UseDDE True if you want to use DDE
False if you want to use ODBC
PrintDoc True if you want to print the result of the merge
False if you only want to view the result of the merge
To create the sample procedure, follow these steps:
Option Explicit
Sub CreateMergeDoc(UseDDE As Boolean, PrintDoc As Boolean)
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim strLetter As String
Dim strConnect As String
' Create an instance of Microsoft Word 97.
Set WordApp = CreateObject("Word.Application")
' Create a new, empty document.
Set WordDoc = WordApp.Documents.Add
With WordDoc.MailMerge
If UseDDE Then
strConnect = "TABLE Customers"
Else
' Note that on your computer the path
' to Northwind.mdb may be different.
' You can set DSN=MS Access 97
' in Microsoft Access 7.0.
strConnect = "DSN=MS Access 97 " _
& "Database;DBQ=C:\Program Files\Microsoft Office\" _
& "Office\Samples\Northwind.mdb;" _
& "FIL=MS Access;"
End If
' Note that on your computer the path
' to Northwind.mdb may be different.
.OpenDataSource _
Name:="C:\Program Files\Microsoft Office\Office\" _
& "\Samples\Northwind.mdb", _
ReadOnly:=True, LinkToSource:=True, _
Connection:=strConnect, _
SQLStatement:="SELECT * FROM [Customers]"
' Define the Merge fields in the document.
With .Fields
.Add Range:=WordApp.Selection.Range, Name:="CompanyName"
WordApp.Selection.TypeParagraph
.Add Range:=WordApp.Selection.Range, Name:="Address"
WordApp.Selection.TypeParagraph
.Add Range:=WordApp.Selection.Range, Name:="City"
WordApp.Selection.TypeText Text:=", "
.Add Range:=WordApp.Selection.Range, Name:="Region"
WordApp.Selection.TypeText Text:=" "
.Add Range:=WordApp.Selection.Range, Name:="PostalCode"
WordApp.Selection.TypeParagraph
.Add Range:=WordApp.Selection.Range, Name:="Country"
End With
End With
' Define the body of the letter in the merge document.
strLetter = "Thank you for your business during the past year."
With WordApp.Selection
.TypeParagraph
.TypeParagraph
.TypeText Text:=strLetter
.TypeParagraph
.TypeParagraph
.TypeText Text:="Sincerely,"
.TypeParagraph
.TypeParagraph
.TypeText Text:="Northwind Traders"
End With
With WordDoc.MailMerge
' Only merge records 1-10 from the table.
.DataSource.FirstRecord = 1
.DataSource.LastRecord = 10
' Merge the data to a new document.
.Destination = wdSendToNewDocument
' Execute the mail merge.
.Execute
' If user specified to print the document, disable
' background printing, and then print the merged document.
If PrintDoc Then
.Application.Options.PrintBackground = False
.Application.ActiveDocument.PrintOut
End If
End With
' Show the instance of Microsoft Word.
WordApp.Visible = True
End Sub
CreateMergeDoc UseDDE:=False, PrintDoc:=False
159328 ACC: Using Automation to Run Word 97 Mail Merge from MS Access
154571 ACC95: Running Word Mail Merge from Access Using OLE Automation
Keywords: kbcode kbhowto kbinterop kbprogramming KB170988