Range.InsertDatabase method (Word)

Retrieves data from a data source (for example, a separate Microsoft Word document, a Microsoft Excel worksheet, or a Microsoft Access database) and inserts the data as a table in place of the specified range.

Syntax

expression.InsertDatabase (Format, Style, LinkToSource, Connection, SQLStatement, SQLStatement1, PasswordDocument, PasswordTemplate, WritePasswordDocument, WritePasswordTemplate, DataSource, From, To, IncludeFields)

expression Required. A variable that represents a Range object.

Parameters

Name Required/Optional Data type Description
Format Optional Variant A format listed in the Formats box in the Table AutoFormat dialog box (Table menu). Can be any of the WdTableFormat constants. A border is applied to the cells in the table by default.
Style Optional Variant The attributes of the AutoFormat specified by Format that are applied to the table.
LinkToSource Optional Variant True to establish a link between the new table and the data source.
Connection Optional Variant A range within which to perform the query specified by SQLStatement.
SQLStatement Optional String An optional query string that retrieves a subset of the data in a primary data source to be inserted into the document.
SQLStatement1 Optional String If the query string is longer than 255 characters, SQLStatement denotes the first portion of the string and SQLStatement1 denotes the second portion.
PasswordDocument Optional Variant The password (if any) required to open the data source. (See Remarks below.)
PasswordTemplate Optional Variant If the data source is a Word document, this argument is the password (if any) required to open the attached template. (See Remarks below.)
WritePasswordDocument Optional Variant The password required to save changes to the document. (See Remarks below.)
WritePasswordTemplate Optional Variant The password required to save changes to the template. (See Remarks below.)
DataSource Optional Variant The path and file name of the data source.
From Optional Variant The number of the first record in the range of records to be inserted.
To Optional Variant The number of the last record in the range of records to be inserted.
IncludeFields Optional Variant True to include field names from the data source in the first row of the new table.

Security

Important

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 Office solution developers.

Remarks

The value of the Style argument can be the sum of any combination of the following values:

Value Description
0 (zero) None
1 Borders
2 Shading
4 Font
8 Color
16 Auto Fit
32 Heading Rows
64 Last Row
128 First Column
256 Last Column

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

  • When retrieving data through Open Database Connectivity (ODBC), you specify a connection string.

  • When retrieving data from Excel by using dynamic data exchange (DDE), you specify a named range or "Entire Spreadsheet."

  • When retrieving data from Access, you specify the word "Table" or "Query" followed by the name of a table or query.

Example

This example inserts an Excel spreadsheet named "Data.xls" after the selection. The Style value (191) is a combination of the numbers 1, 2, 4, 8, 16, 32, and 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

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.