ACC2000: How to Use Automation to Create a Word 2000 Merge Document| Article ID | : | 209882 | | Last Review | : | June 30, 2004 | | Revision | : | 2.0 |
This article was previously published under Q209882 Advanced: Requires expert coding, interoperability, and multiuser skills.
This article applies only to a Microsoft Access database (.mdb).
SUMMARY
This article shows you how to use Automation to insert merge fields in a Microsoft Word 2000 mail merge document, and then run the merge.
Back to the top
MORE INFORMATION
In Microsoft Access, you can use Automation to run a mail merge in a
Microsoft Word 2000 document. The sample procedure in this article uses the
OpenDataSource and Execute methods of the MailMerge object that is available in Word 2000.
The following example creates a procedure called CreateMergeDoc, which
takes two arguments:
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:
| 1. | Start Microsoft Access and open a new blank database.
| | 2. | Create a new module, and then type the following line in the Declarations section if it is not already there: Option Explicit | | 3. | On the Tools menu, click References.
| | 4. | In the Available References box, click to select the Microsoft Word 9.0 Object Library check box, and then click OK.
| | 5. | Type or paste the following procedure:
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 2000.
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.
strConnect = "DSN=MS Access " _
& "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
| | 6. | To test this procedure, type the following line in the Immediate window, and then press ENTER:
CreateMergeDoc UseDDE:=False, PrintDoc:=False
Note that Microsoft Word opens with a new mail merge document. |
Back to the top
REFERENCESFor additional information about using Automation to open an existing MicrosoftWord 2000 mail merge document, click the article number below
to view the article in the Microsoft Knowledge Base:
209976 (http://support.microsoft.com/kb/209976/EN-US/) ACC2000: How to Use Automation to Run Word 2000 Mail Merge from Access
Back to the top
APPLIES TO| • | Microsoft Access 2000 Standard Edition | | • | Microsoft Word 2000 Standard Edition |
Back to the top
| Other Support Options- Need More Help?
Contact a Support professional by E-mail, Online or Phone. - Customer Service
For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more. - Newsgroups
Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.
|
|