MailMerge.CreateDataSource Method (Word)

Creates a Microsoft Word document that uses a table to store data for a mail merge.

Syntax

expression .CreateDataSource(Name, PasswordDocument, WritePasswordDocument, HeaderRecord, MSQuery, SQLStatement, SQLStatement1, Connection, LinkToSource)

expression Required. A variable that represents a MailMerge object.

Parameters

Name

Required/Optional

Data Type

Description

Name

Optional

Variant

The path and file name for the new data source.

PasswordDocument

Optional

Variant

The password required to open the new data source.

WritePasswordDocument

Optional

Variant

The password required to save changes to the data source.

HeaderRecord

Optional

Variant

Field names for the header record. If this argument is omitted, the standard header record is used: "Title, FirstName, LastName, JobTitle, Company, Address1, Address2, City, State, PostalCode, Country, HomePhone, WorkPhone." To separate field names, use the list separator specified in Regional Settings in Control Panel.

MSQuery

Optional

Variant

True to launch Microsoft Query, if it is installed. The Name, PasswordDocument, and HeaderRecord arguments are ignored.

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.

Connection

Optional

Variant

A range within which the query specified by SQLStatement will be performed.

LinkToSource

Optional

Variant

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

Security

Avoid using hard-coded passwords in your applications. If a password is required in a procedure, request the password from the user, store it in a variable, and then use the variable in your code. For recommended best practices on how to do this, see Security Notes for Microsoft Office Solution Developers.

Remarks

When you use the CreateDataSource method, Word attaches the new data source to the specified document, which becomes a main document, if it is not one already.

How you specify the range for the Connection argument depends on how data is retrieved. For example:

  • When retrieving data through ODBC, you specify a connection string.

  • When retrieving data from Microsoft Office Excel using dynamic data exchange (DDE), you specify a named range.

    Security noteSecurity Note
    Dynamic data exchange (DDE) is an older technology that is not secure. If possible, use a more secure alternative to DDE, such as object linking and embedding (OLE).
  • When retrieving data from Microsoft Office Access, you specify the word "Table" or "Query" followed by the name of a table or query.

Example

This example creates a new data source document named "Data.doc" and attaches the data source to the active document. The new data source includes a five-column table that has the field names specified by the HeaderRecord argument.

ActiveDocument.MailMerge.CreateDataSource _ 
 Name:="C:\Documents\Data.doc", _ 
 HeaderRecord:="Name, Address, City, State, Zip"

See Also

Concepts

MailMerge Object

MailMerge Object Members