Microsoft KB Archive/279462

= PRB: Prompted To Select a Data Source While Automating Word 2002 Mail Merge =

Article ID: 279462

Article Last Modified on 10/23/2006

-

APPLIES TO


 * Microsoft Word 2002 Standard Edition

-



This article was previously published under Q279462



SYMPTOMS
When you call the OpenDataSource method while automating Word 2002, the Select Data Source dialog box appears. If Word is not visible, it may appear to have stopped responding.



CAUSE
When you call OpenDataSource, the Select Data Source dialog box typically appears if you have not provided enough information to connect to the data source. By default, Word 2002 attempts to connect to the data source by using the Office DataSource Object (ODSO). ODSO requires the complete path and file name to either a database or an Office DataSource Connection (.odc) file for the Name argument. If you provide the Name argument with an empty string, the Select Data Source dialog box appears.



RESOLUTION
To avoid the dialog box to select a data source, do one of the following:
 * Supply the Name argument of OpenDataSource with the full path to a file-based database, such as a Microsoft Access database, a Microsoft Excel workbook, or a text file.
 * Supply the Name argument of OpenDataSource with the full path to an .odc file.
 * Supply the SubType argument of OpenDataSource with wdMergeSubTypeWord2000 to emulate Word 2000 behavior.

For additional information on the different data access methods you can use for a Word 2002 mail merge, click the article number below to view the article in the Microsoft Knowledge Base:

285332 HOWTO: Automate Word 2002 with Visual Basic to Create a Mail Merge



MORE INFORMATION
With Word 97 and Word 2000, the following Automation code creates and executes a mail merge against SQL Server data without user intervention.

Note You must change UID and pwd = to the correct values before you run this code. Make sure that UID has the appropriate permissions to perform this operation on the database. Dim oApp As Word.Application Dim oDoc As Word.Document

'Start a new document in Word. Set oApp = New Word.Application 'Make Word visible (to see the dialog box). oApp.Visible = True

Set oDoc = oApp.Documents.Add With oDoc.MailMerge .MainDocumentType = wdFormLetters

'Set up the data source for the mail merge. Dim sSQL As String Dim sConn As String sSQL = &quot;Select au_id, au_lname, au_fname from pubs.dbo.authors&quot; sConn = &quot;DSN=YourSQLServer;DATABASE=pubs;uid= ;pwd= ;&quot; .OpenDataSource Name:=&quot;&quot;, Connection:=sConn, SQLStatement:=sSQL

'Set up the document with the fields. .EditMainDocument .Fields.Add Range:=oApp.Selection.Range, Name:=&quot;au_id&quot; oApp.Selection.TypeParagraph .Fields.Add Range:=oApp.Selection.Range, Name:=&quot;au_lname&quot; oApp.Selection.TypeParagraph .Fields.Add Range:=oApp.Selection.Range, Name:=&quot;au_fname&quot;

'Execute the merge. .Destination = wdSendToNewDocument .Execute End With

'Clean up. Set oDoc = Nothing Set oApp = Nothing With Word 2002, however, the Select Data Source dialog box appears when the OpenDataSource method is called because the Name argument is an empty string. To correct this problem so that the mail merge can execute without user intervention, change the following line in the sample code .OpenDataSource Name:=&quot;&quot;, Connection:=sConn, SQLStatement:=sSQL to: .OpenDataSource Name:=&quot;&quot;, Connection:=sConn, SQLStatement:=sSQL, _ SubType:= wdMergeSubTypeWord2000

