OpenDataSource Method

Microsoft Word Visual Basic

Show All

OpenDataSource Method

       

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

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

expression   Required. An expression that returns a MailMerge object.

Name   Required String. The data source file name. 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 WdOpenFormat constants. To specify an external file format, use the OpenFormat property with the FileConverter object to determine the value to use with this argument.

WdOpenFormat can be one of these WdOpenFormat constants.
wdOpenFormatAllWord
wdOpenFormatAuto Default.
wdOpenFormatDocument
wdOpenFormatEncodedText
wdOpenFormatRTF
wdOpenFormatTemplate
wdOpenFormatText
wdOpenFormatUnicodeText
wdOpenFormatWebPages

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, 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, 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.

OpenExclusive  Optional Variant. True to open exclusively.

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

Example

This example creates a new main document and attaches the Orders table from a Microsoft Access database named “Northwind.mdb.”

Dim docNew As Document

Set docNew = Documents.Add

With docNew.MailMerge
    .MainDocumentType = wdFormLetters
    .OpenDataSource _
        Name:="C:\Program Files\Microsoft Office" & _
        "\Office\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."

Dim docNew As Document

Set docNew = Documents.Add

With docNew.MailMerge
    .MainDocumentType = wdCatalog
    .OpenDataSource Name:="C:\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.

Dim strConnection As String

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