Word (的 Range.InsertDatabase 方法)

擷取資料的資料來源 (例如不同的 Microsoft Word 文件、 Microsoft Excel 工作表或 Microsoft Access 資料庫) 並將資料插入以取代指定範圍的表格。

語法

運算式InsertDatabase (FormatStyleLinkToSourceConnectionSQLStatementSQLStatement1PasswordDocumentPasswordTemplateWritePasswordDocumentWritePasswordTemplateDataSourceFromToIncludeFields)

需要 expression。 代表 Range 物件的變數。

參數

名稱 必要/選用 資料類型 描述
Format 選用 Variant [表格自動格式化] 對話方塊中 [格式] 方塊中所列的格式 ([資料表] 功能表) 。 可以是任何 WdTableFormat 常數。 預設值加上框線套用至表格的儲存格。
Style 選用 Variant Format 指定之自動格式設定的屬性 (這些屬性會套用至表格)。
LinkToSource 選用 Variant True 可建立新表格與資料來源之間的連結。
Connection 選用 Variant 要在其中執行 SQLStatement所指定查詢的範圍。
SQLStatement 選用 String 選用的查詢字串,此字串會在主要資料來源中擷取即將插入至文件的資料子集合。
SQLStatement1 選用 字串 如果查詢字串超過 255 個字元, SQLStatement 會表示字串的第一個部分, 而 SQLStatement1 代表第二個部分。
PasswordDocument 選用 Variant 如果開啟資料來源需要任何) ,密碼 (。 請參閱下面的<備註>。
PasswordTemplate 選用 Variant 如果資料來源是 Word 檔,則如果開啟附加的範本需要任何) ,則此引數為密碼 (。 請參閱下面的<備註>。
WritePasswordDocument 選用 Variant 將變更儲存至檔所需的密碼。 請參閱下面的<備註>。
WritePasswordTemplate 選用 Variant 將變更儲存至範本所需的密碼。 請參閱下面的<備註>。
DataSource 選用 Variant 資料來源的路徑和檔名。
From 選用 Variant 記錄範圍中要插入之第一筆記錄的編號。
To 選用 Variant 記錄範圍中要插入之最後一筆記錄的編號。
IncludeFields 選用 Variant True 是表示 將資料來源的欄位名稱加入新表格的第一列。

安全性

重要事項

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. 如需如何執行這項操作的建議最佳做法,請參閱 Office 解決方案開發人員的安全性注意事項

註解

Style 引數的值可以是下列任何組合值的總和:

描述
0 (零)
1 框線
2 網底
4 字型
8 色彩
16 自動調整
32 標題列
64 末列
128 首欄
256 末欄

您指定 Connection 引數的方式會取決於擷取資料的方式。 例如:

  • 透過開放式資料庫連接 (ODBC) 擷取資料時,您會指定連接字串。

  • 使用動態資料交換 (DDE) 從 Excel 擷取資料時,您會指定一個指定的範圍或「整張工作表」。

  • 從 Access 擷取資料時,您會指定 "Table" 或 "Query" 一字,後面接著表格或查詢的名稱。

範例

這個範例會在選取範圍後面插入名為 "Data.xls" 的 Excel 工作表。 Style 值 (191) 是數字 1、2、4、8、16、32 及 128 的組合。

With Selection 
    .Collapse Direction:=wdCollapseEnd 
    .Range.InsertDatabase _ 
        Format:=wdTableFormatSimple2, Style:=191, _ 
        LinkToSource:=False, Connection:="Entire Spreadsheet", _ 
        DataSource:="C:\MSOffice\Excel\Data.xls" 
End With

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應