MailMerge.OpenDataSource method (Publisher)

Attaches a data source to the specified publication, which becomes a main publication if it is not one already.

Syntax

expression.OpenDataSource (bstrDataSource, bstrConnect, bstrTable, fOpenExclusive, fNeverPrompt)

expression A variable that represents a MailMerge object.

Parameters

Name Required/Optional Data type Description
bstrDataSource Optional String The data source path and file name. You can specify a Microsoft Query (.qry) file instead of specifying a data source, a connection string, and a table name string; values in a Microsoft Query file override values for bstrConnect and bstrTable.
bstrConnect Optional String A connection string.
bstrTable Optional String The name of the table in the data source.
fOpenExclusive Optional Long True to deny others access to the database. False allows others read/write permission to the database. The default value is False.
fNeverPrompt Optional Long True never prompts when opening the data source. False displays the Data Link Properties dialog box. The default value is False.

Remarks

If you are using a data source for mail merge, you must add a catalog merge area to the publication page before you attach to the data source.

Example

This example attaches a table from a database and denies everyone else write access to the database while it is opened.

For this example to run properly, you must replace PathToFile with a valid file path and TableName with a valid data source table name.

Sub AttachDataSource() 
 
    ActiveDocument.MailMerge.OpenDataSource _ 
        bstrDataSource:="PathToFile", _ 
        bstrTable:="TableName", _ 
        fNeverPrompt:=True, fOpenExclusive:=True 
 
End Sub

Note

For TableName, if an Excel spreadsheet is being opened, TableName must be followed by $. That is, bstrTable:="Sheet1" will not work; bstrTable:="Sheet1$" will work. Following is an example that further clarifies this.

In this example, the data is stored in MySpreadSheet.xlsx, Sheet1, in the same directory as the Publisher file.

Dim strDataFile as String
strDataFile = Application.ActiveDocument.Path & "MySpreadSheet.xlsx"

ActiveDocument.MailMerge.OpenDataSource _ 
    bstrDataSource:=strDataFile, _ 
    bstrTable:="Sheet1$", _ 
    fNeverPrompt:=True, fOpenExclusive:=True 

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.