Microsoft KB Archive/285332

= How to automate Word with Visual Basic to create a Mail Merge =

Article ID: 285332

Article Last Modified on 5/14/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q285332



SUMMARY
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).



Data access methods
To 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:

.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 OLEDB
OLEDB 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 .OpenDataSource Name:=&quot;C:\MyDB.mdb&quot;, _ SQLStatement:=&quot;SELECT * FROM [MyTable]&quot;

- or -

.OpenDataSource Name:=&quot;C:\MyDataSource.odc&quot;, _ SQLStatement:=&quot;SELECT * FROM [MyTable]&quot; Word and other Office XP applications use the Office DataSource Object (ODSO) for OLEDB access to external data sources. ODSO is the only mechanism by which Word can access data by using OLEDB for a mail merge. ODSO requires that the Name argument for OpenDataSource be either a complete path to a database or a complete path to a valid ODC file. ODSO ignores any information in the Connection argument.

Using ODBC
You 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 .OpenDataSource Name:= &quot;&quot;, _ Connection:= &quot;DSN=MySQLServerDSN;DATABASE=pubs;uid=sa;pwd=;&quot;, _ SQLStatement:= &quot;Select au_id, au_lname, au_fname from authors&quot;, _ SubType:= wdMergeSubTypeWord2000

Using DDE
You 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 .OpenDataSource Name:=&quot;C:\MyDB.mdb&quot;, _ SQLStatement:=&quot;SELECT * FROM [MyTable]&quot;, _ SubType:=wdMergeSubTypeWord2000

Automation sample
The 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:  Start a new Standard EXE project in Visual Basic. By default, Form1 is created. On the Project menu, click References. Click Microsoft Word 2000 Object Library in the list of references, and then click OK.

Note: To use the Microsoft Office Word 2003 Object, add the Microsoft Word 11.0 Object Library in the list of references and then Click OK. Add a CommandButton control to Form1.</li>  Add the following code to the code module for Form1.

Note If it is necessary, modify the path to Northwind.mdb to match your installation for Office XP. Dim WithEvents oApp As Word.Application

Private Sub Form_Load 'Start Word. Set oApp = CreateObject(&quot;Word.Application&quot;) End Sub

Private Sub Command1_Click

Dim oMainDoc As Word.Document Dim oSel As Word.Selection Dim sDBPath as String

'Start a new main document for the mail merge. Set oMainDoc = oApp.Documents.Add

With oMainDoc.MailMerge

.MainDocumentType = wdFormLetters 'Set up the mail merge data source to Northwind.mdb. sDBPath = &quot;C:\Program Files\Microsoft Office\&quot; & _ &quot;OfficeXP\Samples\Northwind.mdb&quot; .OpenDataSource Name:=sDBPath, _ SQLStatement:=&quot;SELECT * FROM [Customers]&quot; 'Add the field codes to the document to create the form letter. With .Fields Set oSel = oApp.Selection .Add oSel.Range, &quot;CompanyName&quot; oSel.TypeParagraph .Add oSel.Range, &quot;Address&quot; oSel.TypeParagraph .Add oSel.Range, &quot;City&quot; oSel.TypeText &quot;, &quot; .Add oSel.Range, &quot;Country&quot; oSel.TypeParagraph oSel.TypeParagraph oSel.TypeText &quot;Dear &quot; .Add oSel.Range, &quot;ContactName&quot; oSel.TypeText &quot;,&quot; oSel.TypeParagraph oSel.TypeParagraph oSel.TypeText &quot; This letter is to inform you...&quot; oSel.TypeParagraph oSel.TypeParagraph oSel.TypeText &quot;Sincerely, [Your Name Here]&quot; End With End With 'Perform the mail merge to a new document. With oMainDoc .MailMerge.Destination = wdSendToNewDocument .MailMerge.Execute Pause:=False End With End Sub

Private Sub oApp_MailMergeAfterMerge(ByVal Doc As Word.Document, ByVal DocResult As Word.Document)

'When the mail merge is complete, 1) make Word visible,   '2) close the mail merge document leaving only the resulting document 'open and 3) display a message.   Doc.Close False    oApp.Visible = True    MsgBox &quot;Mail Merge Complete: &quot; & oApp.ActiveDocument.Name End Sub

Private Sub Form_Unload(Cancel As Integer) Set oApp = Nothing End Sub </li> Press F5 to run the program.</li> Click the CommandButton control on Form1 to perform the mail merge.</li></ol>

When the code completes, Word is made visible with a new document open. The new document contains form letters that result from a mail merge containing data that is extracted from the Customers table in Northwind.mdb.

<div class="references_section">