PRB: Prompted To Select a Data Source While Automating Word 2002 Mail Merge (279462)



The information in this article applies to:

  • Microsoft Word 2002

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 <username> and pwd =<strong password> 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 = "Select au_id, au_lname, au_fname from pubs.dbo.authors"
  sConn = "DSN=YourSQLServer;DATABASE=pubs;uid=<username>;pwd=<strong password>;"
  .OpenDataSource Name:="", Connection:=sConn, SQLStatement:=sSQL

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

  '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:="", Connection:=sConn, SQLStatement:=sSQL
				
to:
  .OpenDataSource Name:="", Connection:=sConn, SQLStatement:=sSQL, _
                  SubType:= wdMergeSubTypeWord2000
				

REFERENCES

For more information, see the following article in the Microsoft Knowledge Base:

289830 PRB: Prompt to Select Table with Word 2002 Mail Merge Code for Excel or Access Data Source


Modification Type:MajorLast Reviewed:10/29/2003
Keywords:kbAutomation kbprb KB279462