Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Walkthrough: Binding Data to Controls on a Word Actions Pane

Note Required applications

The features in this topic are available only if you have the required applications installed.

For more information, see Features Available by Product Combination.

  • One of these development environments:

    VSTO 2005

    -or-

    Visual Studio Team System

  • Microsoft Office 2003

This walkthrough demonstrates data binding to controls on an actions pane in Microsoft Office Word 2003. The controls will demonstrate a master/detail relation between tables in a SQL Server database.

During this walkthrough, you will learn how to:

  • Create an actions pane control for the actions pane.

  • Add Windows Forms controls that are bound to data to an actions pane control.

  • Use a master/detail relationship when displaying data in the controls.

  • Show the actions pane when the application opens.

  • Test your project.

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To complete this walkthrough, you will need:

  • Microsoft Visual Studio 2005 Tools for the Microsoft Office System.

  • Microsoft Office Word 2003.

  • Access to a server with the Northwind SQL Server sample database.

  • Permissions to read from and write to the SQL Server database.

In this step, you will create a Word Document project.

To create a new project

  • Create a Word Document project with the name My Word Actions Pane. Make sure that Create a new document is selected. For more information, see How to: Create Visual Studio Tools for Office Projects.

    Visual Studio opens the new Word document in the designer and adds the My Word Actions Pane project to Solution Explorer.

For this walkthrough, you need an actions pane control that contains data-bound Windows Forms controls. Add a data source to the project, and then drag controls from the Data Sources window to the actions pane control.

To add an actions pane control

  1. Select the My Word Actions Pane project in Solution Explorer.

  2. On the Project menu, select Add New Item.

  3. In the Add New Item dialog box, select Actions Pane Control, name it ActionsControl, and then click Add.

To add a new data source to the project

  1. If the Data Sources window is not visible, click Show Data Sources on the Data menu.

    NoteNote

    If Show Data Sources is not available, click inside the Word document and then check again.

  2. Click Add New Data Source to start the Data Source Configuration Wizard.

  3. Select Database and then click Next.

  4. Select a data connection to the Northwind sample SQL Server database, or add a new connection using the New Connection button.

  5. Click Next.

  6. Clear the option to save the connection if it is selected, and then click Next.

  7. Expand the Tables node in the Database objects window.

  8. Select the check box next to the Suppliers and Products tables.

  9. Click Finish.

The wizard adds the Suppliers table and Products table to the Data Sources window. It also adds a typed dataset to your project that is visible in Solution Explorer.

To add data-bound Windows Forms controls to an actions pane control

  1. In the Data Sources window, expand the Suppliers table.

  2. Click the drop-down arrow on the Company Name node, and select ComboBox.

  3. Drag CompanyName from the Data Sources window to the actions pane control.

    A ComboBox control is created on the actions pane control. At the same time, a BindingSource named SuppliersBindingSource, a table adapter, and a DataSet are added to the project in the component tray.

  4. Select the SuppliersBindingNavigator in the Component tray and press DELETE. You will not use the SuppliersBindingNavigator in this walkthrough.

    NoteNote

    Deleting the SuppliersBindingNavigator does not remove all of the code that was generated for it. You can remove this code.

  5. Move the combo box so that it is under the label and change the Size property to 171, 21.

  6. In the Data Sources window, expand the Products table that is a child of the Suppliers table.

  7. Click the drop-down arrow on the ProductName node, and select ListBox.

  8. Drag ProductName to the actions pane control.

    A ListBox control is created on the actions pane control. At the same time, a BindingSource named ProductBindingSource and a table adapter are added to the project in the component tray.

  9. Move the list box so that it is under the label and change the Size property to 171,95.

  10. Drag a Button from the Toolbox onto the actions pane control and place it below the list box.

  11. Right-click the Button, click Properties in the shortcut menu, and change the following properties:

    Property Value

    Name

    Insert

    Text

    Insert

  12. Resize the user control to fit the controls.

In this section, you will add code to the Load event of the actions pane control to fill the control with data from the DataTable. You will then set the DataSource and DataMember properties for each control.

To load the control with data

  1. In the Load event handler of the ActionsControl class, add the following code:

    private void ActionsControl_Load(object sender, EventArgs e)
    {
        this.suppliersTableAdapter.Fill(this.northwindDataSet.Suppliers);
        this.productsTableAdapter.Fill(this.northwindDataSet.Products);
    }
    
    
  2. In C#, you must attach the event handler to the Load event. You can place this code in the ActionsControl constructor, after the call to InitializeComponent. For more information about creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    this.Load += new EventHandler(ActionsControl_Load);
    
    

To set data binding properties of the controls

  1. Select the CompanyNameComboBox control.

  2. In the Properties window, click the button to the right of the DataSource property, and select suppliersBindingSource.

  3. In the Properties window, click the button to the right of the DisplayMember property, and select CompanyName.

  4. In the Properties window, expand the DataBindings property, click the button to the right of the Text property, and select None.

  5. Select the ProductNameListBox control.

  6. In the Properties window, click the button to the right of the DataSource property, and select productsBindingSource.

  7. In the Properties window, click the button to the right of the DisplayMember property, and select ProductName.

  8. In the Properties window, expand the DataBindings property, click the button to the right of the SelectedValue property, and select None.

In this step, you will read the data from the bound controls and populate a table in your Word document. First, you will create a procedure for formatting the headings in the table, and then you will add the AddData method to create and format a Word table.

To format the table headings

  • In the ActionsControl class, create a method to format the headings of the table. Add code as shown in the following example:

    static void SetHeadings(Microsoft.Office.Interop.Word.Cell tblCell, string text)
    {
        tblCell.Range.Text = text;
        tblCell.Range.Font.Bold = 1;
        tblCell.Range.ParagraphFormat.Alignment = 
            Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    }
    
    

To create the table

  • In the ActionsControl class, write a method that will create a table if one does not already exist, and add data from the actions pane to the table. Add code as shown in the following example.

    private void AddData(DataRow row, string companyName)
    {
        object missing = System.Type.Missing;
    
        // Create a table if it doesn't already exist.
        if (Globals.ThisDocument.Tables.Count == 0)
        {
            try
            {
                // Create a table.
                Microsoft.Office.Interop.Word.Table tbl = Globals.ThisDocument.Tables.Add
                    (Globals.ThisDocument.Application.Selection.Range, 1, 4, ref missing, ref missing);
    
                // Insert headings.
                SetHeadings(tbl.Cell(1, 1), "Company Name");
                SetHeadings(tbl.Cell(1, 2), "Product Name");
                SetHeadings(tbl.Cell(1, 3), "Quantity");
                SetHeadings(tbl.Cell(1, 4), "Unit Price");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem creating Products table: " + ex.Message, 
                    "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    
        // Add data from data row to the table.
        Microsoft.Office.Interop.Word.Selection selection = Globals.ThisDocument.Application.Selection;
    
        if (selection.Tables.Count > 0)
        {
            Microsoft.Office.Interop.Word.Row newRow = Globals.ThisDocument.Tables[1].Rows.Add(ref missing);
    
            newRow.Range.Font.Bold = 0;
    
            newRow.Range.ParagraphFormat.Alignment = 
                Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
    
            newRow.Cells[4].Range.ParagraphFormat.Alignment =
                Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
    
            newRow.Cells[1].Range.Text = companyName;
            newRow.Cells[2].Range.Text = row["ProductName"].ToString();
            newRow.Cells[3].Range.Text = row["QuantityPerUnit"].ToString();
            newRow.Cells[4].Range.Text = Math.Round(Convert.ToDouble(row["UnitPrice"])).ToString("#,##0.00");
        }
        else
        {
            MessageBox.Show("Cursor must be within a table.", 
                "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    
    

To insert text into a Word table

  1. In the Click event handler of the Insert button, add code as shown in the following example:

    NoteNote

    For information on creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    private void Insert_Click(object sender, System.EventArgs e)
    {
        DataTable tbl = northwindDataSet.Products;
        DataRow[] rows;
    
        // Check if a product is selected.
        if (this.productNameListBox.SelectedIndex >= 0)
        {
            DataRowView productRow =(System.Data.DataRowView)this.productNameListBox.SelectedItem;
    
            string product = productRow.Row["ProductName"].ToString();
            string company = this.companyNameComboBox.Text;
    
            // Return the data row from the selected product.
            rows = tbl.Select("ProductName = '" + product.Replace("'", "''") + "'");
    
            this.AddData(rows[0], company);
        }
        else
        {
            MessageBox.Show("Please select a product.", "Actions Pane", MessageBoxButtons.OK);
        }
    }
    
    
  2. In C#, you must create an event handler for the Click event of the button. You can place this code in the Load event handler of the ActionsControl class. For more information on creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    this.Insert.Click += new EventHandler(Insert_Click);
    
    

In this section, you will write code to show the actions pane. The actions pane becomes visible after controls are added to it.

To show the actions pane

  1. In Solution Explorer, right-click ThisDocument.vb or ThisDocument.cs, and then click View Code on the shortcut menu.

  2. Create a new instance of the control at the top of the ThisDocument class so that it looks like the following example:

    private ActionsControl actions = new ActionsControl();
    
    
  3. Add code to the Startup event handler of ThisDocument so that it looks like the following example:

    NoteNote

    For information on creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    this.ActionsPane.Controls.Add(actions);
    
    

Now you can test your document to make sure that the actions pane opens when the document is opened. Test for the master/detail relationship in the controls on the actions pane, and make sure that data is populated in a Word table when the Insert button is clicked.

To test your document

  1. Press F5 to run your project.

  2. Confirm that the actions pane is visible.

  3. Select a company in the combo box and verify that the items in the Products list box change.

  4. Select a product, click Insert on the actions pane, and verify that the product details are added to the table in Word.

  5. Insert additional products from various companies.

This walkthrough shows the basics of binding data to controls on an actions pane in Word. Here are some tasks that might come next:

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft