OpenDataSource 方法 (Word)MailMerge.OpenDataSource method (Word)

将一个数据源附加到指定文档,如果该文档不是主文档,则将变成主文档。Attaches a data source to the specified document, which becomes a main document if it is not one already.


expressionexpression. OpenDataSource( _Name_ , _Format_ , _ConfirmConversions_ , _ReadOnly_ , _LinkToSource_ , _AddToRecentFiles_ , _PasswordDocument_ , _PasswordTemplate_ , _Revert_ , _WritePasswordDocument_ , _WritePasswordTemplate_ , _Connection_ , _SQLStatement_ , _SQLStatement1_ , _OpenExclusive_ , _SubType_ )

expression:必需。expression Required. 一个代表 "邮件合并" 对象的变量。A variable that represents a 'MailMerge' object.


名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
NameName 必需Required StringString 数据源文件名。The data source file name. 可以指定一个 Microsoft Query (.qry) 文件,而不必指定数据源、连接字符串及查询字符串。You can specify a Microsoft Query (.qry) file instead of specifying a data source, a connection string, and a query string.
FormatFormat 可选Optional VariantVariant 用于打开文档的文件转换器。The file converter used to open the document. 可以是 WdOpenFormat 常量之一。Can be one of the WdOpenFormat constants. 要指定外部文件格式,可将 OpenFormat 属性与 FileConverter 对象结合使用以确定用于该参数的值。To specify an external file format, use the OpenFormat property with the FileConverter object to determine the value to use with this argument.
ConfirmConversionsConfirmConversions 可选Optional VariantVariant
如果该属性值为 **True**,则当文件不是 Microsoft Word 格式时,将显示 “转换文件” 对话框。True to display the Convert File dialog box if the file is not in Microsoft Word format.
ReadOnlyReadOnly 可选Optional VariantVariant
如果该属性值为 **True**,则以只读方式打开数据源。True to open the data source on a read-only basis.
LinkToSourceLinkToSource 可选Optional VariantVariant 真正 执行查询指定连接,则每次打开主文档。True to perform the query specified by Connection and SQLStatement each time the main document is opened.
AddToRecentFilesAddToRecentFiles 可选Optional VariantVariant 要将文件名添加到列表中最近使用的文件在 文件菜单的底部。True to add the file name to the list of recently used files at the bottom of the File menu.
PasswordDocumentPasswordDocument 可选Optional VariantVariant 用于打开数据源的密码。The password used to open the data source. 请参阅下面的"说明")。(See Remarks below.)
PasswordTemplatePasswordTemplate 可选Optional VariantVariant 用于打开模板的密码。The password used to open the template. 请参阅下面的"说明")。(See Remarks below.)
RevertRevert 可选Optional VariantVariant 控制如果 Name 是已打开文档的文件名,会发生什么情况。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.
WritePasswordDocumentWritePasswordDocument 可选Optional VariantVariant 用于保存对文档所做的更改的密码。The password used to save changes to the document. 请参阅下面的"说明")。(See Remarks below.)
WritePasswordTemplateWritePasswordTemplate 可选Optional VariantVariant 用于保存对模板所做的更改的密码。The password used to save changes to the template. 请参阅下面的"说明")。(See Remarks below.)
ConnectionConnection 可选Optional VariantVariant 一个范围, SQLStatement 指定的查询将在该范围内执行。A range within which the query specified by SQLStatement is to be performed. 请参阅下面的"说明")。(See Remarks below.)
SQLStatementSQLStatement 可选Optional VariantVariant 定义查询选项以检索数据。Defines query options for retrieving data. 请参阅下面的"说明")。(See Remarks below.)
SQLStatement1SQLStatement1 可选Optional VariantVariant 如果查询字符串超过 255 个字符,则 SQLStatement 指定字符串的第一部分,SQLStatement1 指定第二部分。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.)
OpenExclusiveOpenExclusive 可选Optional VariantVariant 真正 以独占方式打开。True to open exclusively.
SubTypeSubType 可选Optional VariantVariant 可以是WdMergeSubType常量之一。Can be one of the WdMergeSubType constants.


若要确定 ODBC 连接和查询字符串, 请手动设置查询选项, 并使用QueryString属性返回连接字符串。To determine the ODBC connection and query strings, set query options manually and use the QueryString property to return the connection string. 下表包含了一些常用的 SQL 关键字。The following table includes some commonly used SQL keywords.

关键字Keyword 说明Description
DSNDSN ODBC 数据源的名称The name of the ODBC data source
UIDUID 用户登录 IDThe user logon ID
PWDPWD 用户指定的密码The user-specified password
DBQDBQ 数据库文件名The database file name
FILFIL 文件类型The file type

若要指示 Word 使用与早期版本的 word (动态数据交换 (DDE)) 中的相同连接方法 (microsoft office Access 和 microsoft office Excel 数据源) SubType:=wdMergeSubTypeWord2000, 请使用。To instruct Word to use the same connection method as in earlier versions of Word (Dynamic Data Exchange (DDE) for Microsoft Office Access and Microsoft Office Excel data sources) use SubType:=wdMergeSubTypeWord2000.

指定区域的方式取决于检索数据的方式。How you specify the range depends on how data is retrieved. 例如:For example:

  • 当通过开放式数据库连接 (Open Database Connectivity, ODBC) 检索数据时,请指定一个连接字符串。When retrieving data through Open Database Connectivity (ODBC), you specify a connection string.

  • 当使用动态数据交换 (DDE) 检索 Excel 的数据时,请指定一个已命名的区域。When retrieving data from Excel using dynamic data exchange (DDE), you specify a named range. 安全说明Security Note

  • 当检索 Access 的数据时,请指定单词"Table"或"Query",后面紧接表格或查询名称。When retrieving data from Access, you specify the word "Table" or "Query" followed by the name of a table or query.

安全说明Security Note

安全说明Security Note

尽量避免在应用程序中使用硬编码的密码。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. 有关执行此操作的建议最佳实践,请参阅 Microsoft Office 解决方案开发人员安全注意事项For recommended best practices on how to do this, see Security Notes for Microsoft Office Solution Developers.


本示例新建一个主文档并附加 Orders 表,该表来自名为“Northwind.mdb”的 Access 数据库。This example creates a new main document and attaches the Orders table from an 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

本示例创建一个新的主文档, 并将其附加到 Excel 工作表中。名称 .xls.?This example creates a new main document and attaches the Excel worksheet named ?Names.xls.? Connection 参数用来检索名为“Sales”的区域中的数据。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, _ 
End With

本示例用 ODBC 将名为“Northwind.mdb”的 Access 数据库附加到活动文档。This example uses ODBC to attach the Access database named "Northwind.mdb" to the active document. SQLStatement 参数用来在 Customers 表中选择记录。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

另请参阅See also

邮件合并对象MailMerge Object

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.