共用方式為


Bookmark.InsertDatabase Method

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

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word (in Microsoft.Office.Tools.Word.dll)

Syntax

'Declaration
Sub InsertDatabase ( _
    ByRef Format As Object, _
    ByRef Style As Object, _
    ByRef LinkToSource As Object, _
    ByRef Connection As Object, _
    ByRef SQLStatement As Object, _
    ByRef SQLStatement1 As Object, _
    ByRef PasswordDocument As Object, _
    ByRef PasswordTemplate As Object, _
    ByRef WritePasswordDocument As Object, _
    ByRef WritePasswordTemplate As Object, _
    ByRef DataSource As Object, _
    ByRef From As Object, _
    ByRef To As Object, _
    ByRef IncludeFields As Object _
)
void InsertDatabase(
    ref Object Format,
    ref Object Style,
    ref Object LinkToSource,
    ref Object Connection,
    ref Object SQLStatement,
    ref Object SQLStatement1,
    ref Object PasswordDocument,
    ref Object PasswordTemplate,
    ref Object WritePasswordDocument,
    ref Object WritePasswordTemplate,
    ref Object DataSource,
    ref Object From,
    ref Object To,
    ref Object IncludeFields
)

Parameters

  • Format
    Type: System.Object%

    A format listed in the Formats box in the Table AutoFormat dialog box. Can be any of the WdTableFormat constants. A border is applied to the cells in the table by default.

  • Style
    Type: System.Object%

    The attributes of the AutoFormat specified by Format that are applied to the table. Use the sum of any combination of the following values:

    [1|1] Value

    [1|2] Meaning

    [2|1] 0 (zero)

    [2|2] None

    [3|1] 1

    [3|2] Borders

    [4|1] 2

    [4|2] Shading

    [5|1] 4

    [5|2] Font

    [6|1] 8

    [6|2] Color

    [7|1] 16

    [7|2] Auto Fit

    [8|1] 32

    [8|2] Heading Rows

    [9|1] 64

    [9|2] Last Row

    [10|1] 128

    [10|2] First Column

    [11|1] 256

    [11|2] Last Column

  • LinkToSource
    Type: System.Object%

    true to establish a link between the new table and the data source.

  • Connection
    Type: System.Object%

    A range within which to perform the query specified by SQLStatement. How you specify the range 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 Microsoft Office Excel by using dynamic data exchange (DDE), you specify a named range or "Entire Spreadsheet."

    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 Access, you specify the word "Table" or "Query" followed by the name of a table or query.

  • SQLStatement
    Type: System.Object%

    An optional query string that retrieves a subset of the data in a primary data source to be inserted into the document.

  • SQLStatement1
    Type: System.Object%

    If the query string is longer than 255 characters, SQLStatement denotes the first portion of the string and SQLStatement1 denotes the second portion.

  • PasswordDocument
    Type: System.Object%

    The password (if any) required to open the data source. (See Remarks below.)

  • PasswordTemplate
    Type: System.Object%

    If the data source is a Microsoft Office Word document, this argument is the password (if any) required to open the attached template. (See Remarks below.)

  • WritePasswordDocument
    Type: System.Object%

    The password required to save changes to the document. (See Remarks below.)

  • WritePasswordTemplate
    Type: System.Object%

    The password required to save changes to the template. (See Remarks below.)

  • DataSource
    Type: System.Object%

    The path and file name of the data source.

  • From
    Type: System.Object%

    The number of the first data record in the range of records to be inserted.

  • To
    Type: System.Object%

    The number of the last data record in the range of records to be inserted.

  • IncludeFields
    Type: System.Object%

    true to include field names from the data source in the first row of the new table.

Remarks

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.

Avoid using the built-in system administrator (sa) logon account. Instead, make system administrators members of the sysadmin fixed server role, and have them use their own accounts to log on. Use sa only when there is no other way to log on. To prevent unauthorized access through the sa logon account, you should assign that account a strong, unique password.

When possible, use Windows Authentication (also referred to as a trusted connection), which uses a Windows user account to connect to SQL Server. When a user connects through a Windows user account, SQL Server uses information in the Windows operating system to validate the account name and password. Before you can use Windows Authentication, a server administrator must configure SQL Server to use this mode of authentication. If Windows Authentication is not available, avoid saving users' logon information. It is more secure for users to enter their logon information each time they log on.

Optional Parameters

For information on optional parameters, see Optional Parameters in Office Solutions.

Examples

This example inserts a Microsoft Excel spreadsheet into the Bookmark control. The Style value (191) is a combination of the numbers 1, 2, 4, 8, 16, 32, and 128. This example assumes you have a workbook named Data.xlsx saved to the root of the C directory with at least two rows of data on the worksheet.

This example is for a document-level customization.

Private Sub BookmarkInsertDatabase()

    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Dim Bookmark1 As Microsoft.Office.Tools.Word.Bookmark = _
        Me.Controls.AddBookmark(Me.Paragraphs(1).Range, "Bookmark1")

    Bookmark1.Text = "This is sample bookmark text"
    Bookmark1.InsertDatabase(Format:=Word.WdTableFormat _
        .wdTableFormatClassic1, Style:=191, LinkToSource:=False, _
        Connection:="Entire Spreadsheet", DataSource:="C:\Data.xlsx")

End Sub
private void BookmarkInsertDatabase()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    Microsoft.Office.Tools.Word.Bookmark bookmark1 =
        this.Controls.AddBookmark(this.Paragraphs[1].Range,
        "bookmark1");
    bookmark1.Text = "This is sample bookmark text";

    object Format = Word.WdTableFormat.wdTableFormatClassic1;
    object Style = 191;
    object LinkToSource = false;
    object Connection = "Entire Spreadsheet";
    object DataSource = "C:\\Data.xlsx";

    bookmark1.InsertDatabase(ref Format, ref Style, ref LinkToSource,
        ref Connection, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref DataSource,
        ref missing, ref missing, ref missing);
}

.NET Framework Security

See Also

Reference

Bookmark Interface

Microsoft.Office.Tools.Word Namespace