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

ActiveDocument.MailMerge.OpenDataSource Name:=  "C:\Temp\mergeSource.csv" 
                                        ConfirmConversions:=False, _
                                        ReadOnly:=False, _
                                        LinkToSource:=True, _
                                        AddToRecentFiles:=False, _
                                        PasswordDocument:="", _
                                        PasswordTemplate:="", _
                                        Revert:=False, _
                                        WritePasswordDocument:="", _
                                        WritePasswordTemplate:="", _
                                        Connection:="", _
                                        SQLStatement:="", _
                                        SQLStatement1="", _
                                        OpenExclusive:= , _

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. To specify an external file format, use the OpenFormat property with the 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 Microsoft 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. (See Remarks below.)
PasswordTemplate - Optional Variant. The password used to open the template. (See Remarks below.)
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. (See Remarks below.)
WritePasswordTemplate - Optional Variant. The password used to save changes to the template. (See Remarks below.)
Connection - Optional Variant. A range within which the query specified by SQLStatement is to be performed. (See Remarks below.) How you specify the range depends on how data is retrieved. For example:
SQLStatement - Optional Variant. Defines query options for retrieving data. (See Remarks below.)
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. (See Remarks below.)
OpenExclusive - Optional Variant. True to open exclusively.

Attaches a table from an Access database (using DDE)

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

With ActiveDocument.MailMerge 
    .MainDocumentType = wdMailMergeMainDocType.wdFormLetters
    .OpenDataSource Name:="C:\Temp\Northwind.mdb"
                    LinkToSource:=True, _
                    AddToRecentFiles:=False, _
                    Connection:="TABLE Orders"
End With

Attaches a table from an Access database (using ODBC)

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 
    strConnection = "DSN=MS Access Databases;"DBQ=C:\Northwind.mdb;"FIL=RedISAM;"

With ActiveDocument.MailMerge
    .MainDocumentType = wdMailMergeMainDocType.wdFormLetters
    .OpenDataSource Name:="C:\Temp\NorthWind.mdb", _
        Connection:=strConnection, _
        SQLStatement:="SELECT * FROM Customers"
End With

Attaches a named range from an Excel Workbook

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

With ActiveDocument.MailMerge 
    .MainDocumentType = wdMailMergeMainDocType.wdCatalog
    .OpenDataSource Name:="C:\Documents\Names.xls", _
                    ReadOnly:=True, _
End With

Removing a DataSource

ActiveDocument.MailMerge.MainDocumentType = wdMailMergeMainDocType.wdNotAMergeDocument 

© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext