OpenDataSource 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