OpenDataSource Method

Applies To

MailMerge object.

Description

Attaches a data source to the specified document, which becomes a main document if it's not one already.

Syntax

expression.OpenDataSource(Name, Format, ConfirmConversions, ReadOnly,
ú LinkToSource, AddToRecentFiles, PasswordDocument, PasswordTemplate,
ú Revert, WritePasswordDocument, WritePasswordTemplate, Connection,
ú SQLStatement, SQLStatement1)

expression Required. An expression that returns a MailMerge object.

Name Required String. The data source file name. In Windows, you can specify a Microsoft Query (.qry) file instead of specifying a data source, a connection string, and a query string.

Format Optional Variant. The file converter used to open the document. Can be one of the following WdOpenFormat constants: wdOpenFormatAuto, wdOpenFormatDocument, wdOpenFormatRTF, wdOpenFormatTemplate, wdOpenFormatText, or wdOpenFormatUnicodeText. The default value is wdOpenFormatAuto.

To specify an external file format, use the OpenFormat property with a FileConverter object to determine the value to use with this argument.

ConfirmConversions Optional Variant. True to display the Convert File dialog box if the file isn't in Word format.

ReadOnly Optional Variant. True to open the data source on a read-only basis.

LinkToSource Optional Variant. True to perform the query specified by Connection and SQLStatement each time the main document is opened.

AddToRecentFiles Optional Variant. True to add the file name to the list of recently used files at the bottom of the File menu.

PasswordDocument Optional Variant. The password used to open the data source.

PasswordTemplate Optional Variant. The password used to open the template.

Revert Optional Variant. Controls what happens if Name is the file name of an open document. True to discard any unsaved changes to the open document and reopen the file; False to activate the open document.

WritePasswordDocument Optional Variant. The password used to save changes to the document.

WritePasswordTemplate Optional Variant. The password used to save changes to the template.

Connection Optional Variant. A range within which the query specified by SQLStatement is to be performed. How you specify the range depends on how data is retrieved. For example:

  • When retrieving data through ODBC (Windows only), you specify a connection string.
  • When retrieving data from Microsoft Excel using dynamic data exchange (DDE), you specify a named range.
  • When retrieving data from Microsoft Access (Windows only), you specify the word "Table" or "Query" followed by the name of a table or query.
SQLStatement Optional Variant. Defines query options for retrieving data.

SQLStatement1 Optional Variant. If the query string is longer than 255 characters, SQLStatement specifies the first portion of the string, and SQLStatement1 specifies the second portion.

Remarks

To determine the ODBC connection and query strings, set query options manually, and use the QueryString property to return the connection string. The following table includes some commonly used SQL keywords.

Keyword

Description

DSN

The name of the ODBC data source

UID

The user logon ID

PWD

The user-specified password

DBQ

The database file name

FIL

The file type


See Also

ConnectString property, CreateDataSource method, EditDataSource method, QueryString property, UseAddressBook method.

Example

This example creates a new main document and attaches the Customers table from a Microsoft Access database named "Northwind.mdb."

Set myDoc = Documents.Add
With myDoc.MailMerge
    .MainDocumentType = wdFormLetters
    .OpenDataSource Name:="C:\MSOffice\Access\Samples\Northwind.mdb", _
        LinkToSource:=True, AddToRecentFiles:=False, _
        Connection:="TABLE Orders
End With
This example creates a new main document and attaches the Microsoft Excel spreadsheet named "Names.xls." The Connection argument retrieves data from the range named "Sales."

Set myDoc = Documents.Add
With myDoc.MailMerge
    .MainDocumentType = wdCatalog
    .OpenDataSource Name:="C:\My Documents\Names.xls", ReadOnly:=True, _
        Connection:="Sales"
End With
This example uses ODBC to attach the Microsoft Access database named "NorthWind.mdb" to the active document. The SQLStatement argument selects the records in the Customers table.

With ActiveDocument.MailMerge
    .MainDocumentType = wdFormLetters
    constr = "DSN=MS Access Databases;DBQ=C:\Access\NorthWind.mdb;" & "FIL=RedISAM;"
    .OpenDataSource Name:="C:\MSOffice\Access\NorthWind.mdb", _
        Connection:=constr, SQLStatement:="SELECT * FROM Customers"
End With