Select the product you need help with
How to automate Word with Visual Basic to create a Mail MergeArticle ID: 285332 - View products that this article applies to. This article was previously published under Q285332 On This PageSUMMARY This article discusses how to automate Word to create a
mail merge for an external data source. This article also explains the code differences
between accessing the data with OLEDB, ODBC, and dynamic data exchange (DDE).
MORE INFORMATIONData access methodsTo programmatically set up a data source for a Word mail merge document, you first call the OpenDataSource method of a MailMerge object. The syntax for the OpenDataSource method is as follows:
<MailMergeObject>.OpenDataSource(Name, [Format], [ConfirmConversions], [ReadOnly], [LinkToSource], [AddToRecentFiles], [PasswordDocument], [PasswordTemplate], [Revert],[WritePasswordDocument], [WritePasswordTemplate], [Connection], [SQLStatement], [SQLStatement1], [OpenExclusive], [SubType]) Note For a complete description of each argument, refer to the
Microsoft Word Visual Basic online Help. Of primary interest for connecting to
an external data source are the Name, Connection, and SubType arguments. Different combinations of these three arguments
represent different data access methods for the mail merge. Using OLEDBOLEDB is the recommended data access method. To specify OLEDB as the data access method with OpenDataSource, supply the Name argument with the path and the file name to either the database or an Office DataSource Connection (.odc). If you provide a database for the Name argument, Word will automatically use OLEDB if there is an OLEDB provider installed that supports the database format.Example - or - Using ODBCYou can use ODBC for your mail merge to access data for which a user data source name (DSN) has been set up on the system. To specify ODBC as the data access method with OpenDataSource, supply an empty string for the Name argument, an ODBC connection string for the Connection argument, and wdMergeSubTypeWord2000 for the SubType argument.Example Using DDEYou can use DDE to access data in Microsoft Access databases or Microsoft Excel workbooks. To specify DDE as the data access method with OpenDataSource, supply the path and the file name to the database or the workbook for the Name argument, and wdMergeSubTypeWord2000 for the SubType argument.Example Automation sampleThe following sample code creates and executes a mail merge for form letters by using OLEDB (by way of ODSO). The data source that is used is the sample Access database Northwind.mdb. If Northwind is not installed, start Microsoft Access 2002 or Microsoft Office Access 2003. On the Help menu, click Sample Databases, and then choose Northwind Sample Database to install this feature.To run this sample, follow these steps:
REFERENCES
For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
289830
(http://support.microsoft.com/kb/289830/
)
Prompt to select table with Word 2002 mail merge code for Excel or Access data source
279462
(http://support.microsoft.com/kb/279462/
)
Prompted to select a data source while automating Word 2002 mail merge
285333
(http://support.microsoft.com/kb/285333/
)
Word 2002 MailMerge event code demonstration
285176
(http://support.microsoft.com/kb/285176/
)
How to automate Word to perform a client-side Mail Merge using XML from SQL Server
220607
(http://support.microsoft.com/kb/220607/
)
How to automate Microsoft Word to perform mail merge from Visual Basic
Properties | Article Translations
|


Back to the top








