Walkthrough: Binding Data to Controls on a Word Actions Pane
This walkthrough demonstrates data binding to controls on an actions pane in Word. The controls demonstrate a master/detail relation between tables in a SQL Server database.
Applies to: The information in this topic applies to document-level projects for Word. For more information, see Features Available by Office Application and Project Type.
This walkthrough illustrates the following tasks:
Creating an actions pane with Windows Forms controls that are bound to data.
Using a master/detail relationship to display data in the controls.
Show the actions pane when the application opens.
You need the following components to complete this walkthrough:
An edition of Visual Studio that includes the Microsoft Office developer tools. For more information, see Configuring a Computer to Develop Office Solutions.
Word 2013 or Word 2010.
Access to a server with the Northwind SQL Server sample database.
Permissions to read from and write to the SQL Server database.
The first step is to create a Word Document project.
Create a Word Document project with the name My Word Actions Pane. In the wizard, select Create a new document.
For more information, see How to: Create Office Projects in Visual Studio.
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.
Select the My Word Actions Pane project in Solution Explorer.
On the Project menu, click Add New Item.
In the Add New Item dialog box, select Actions Pane Control, name it ActionsControl, and then click Add.
If the Data Sources window is not visible, display it by, on the menu bar, choosing View, Other Windows, Data Sources.
If Show Data Sources is not available, click the Word document and then check again.
Click Add New Data Source to start the Data Source Configuration Wizard.
Select Database and then click Next.
Select a data connection to the Northwind sample SQL Server database, or add a new connection by using the New Connection button.
Clear the option to save the connection if it is selected, and then click Next.
Expand the Tables node in the Database objects window.
Select the check box next to the Suppliers and Products tables.
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.
In the Data Sources window, expand the Suppliers table.
Click the drop-down arrow on the Company Name node, and select ComboBox.
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.
SuppliersBindingNavigatorin the Component tray and press DELETE. You will not use the
SuppliersBindingNavigatorin this walkthrough.
SuppliersBindingNavigatordoes not remove all of the code that was generated for it. You can remove this code.
Move the combo box so that it is under the label and change the Size property to 171, 21.
In the Data Sources window, expand the Products table that is a child of the Suppliers table.
Click the drop-down arrow on the ProductName node, and select ListBox.
Drag ProductName to the actions pane control.
Move the list box so that it is under the label and change the Size property to 171,95.
Drag a Button from the Toolbox onto the actions pane control and place it below the list box.
Right-click the Button, click Properties on the shortcut menu, and change the following properties.
Property Value Name Insert Text Insert
Resize the user control to fit the controls.
In the Load event handler of the
ActionsControlclass, add the following code.
In C#, you must attach the event handler to the Load event. You can place this code in the
ActionsControlconstructor, after the call to
InitializeComponent. For more information about how to create event handlers, see How to: Create Event Handlers in Office Projects.
In the Properties window, click the button to the right of the DataSource property, and select suppliersBindingSource.
Click the button to the right of the DisplayMember property, and select CompanyName.
Expand the DataBindings property, click the button to the right of the Text property, and select None.
In the Properties window, click the button to the right of the DataSource property, and select productsBindingSource.
Click the button to the right of the DisplayMember property, and select ProductName.
Expand the DataBindings property, click the button to the right of the SelectedValue property, and select None.
The next task is to read the data from the bound controls and populate a table in your Word document. First, create a procedure for formatting the headings in the table, and then add the
AddData method to create and format a Word table.
ActionsControlclass, create a method to format the headings of the table.
ActionsControlclass, write a method that will create a table if one does not already exist, and add data from the actions pane to the table.
Private Sub AddData(ByVal row As System.Data.DataRow, ByVal companyName As String) ' Create a table if it doesn't already exist. If Globals.ThisDocument.Tables.Count = 0 Then Try ' Create a table. Dim tbl As Word.Table = Globals.ThisDocument.Tables.Add( _ Globals.ThisDocument.Application.Selection.Range, 1, 4) ' 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 ex As Exception MessageBox.Show("Problem creating Products table: " & ex.Message, _ "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If ' Add data from data row to the table. Dim selection As Word.Selection = Globals.ThisDocument.Application.Selection If selection.Tables.Count > 0 Then Dim newRow As Word.Row = Globals.ThisDocument.Tables(1).Rows.Add() With newRow .Range.Font.Bold = False .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft .Cells(4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight .Cells(1).Range.Text = companyName .Cells(2).Range.Text = row.Item("ProductName").ToString .Cells(3).Range.Text = row.Item("QuantityPerUnit").ToString .Cells(4).Range.Text = Math.Round(row.Item("UnitPrice"), 2) End With Else MessageBox.Show("Cursor must be within a table.", _ "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub
Add the following code to the Click event handler of the Insert button.
Private Sub Insert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Insert.Click Dim tbl As System.Data.DataTable = NorthwindDataSet.Products Dim rows() As System.Data.DataRow ' Check if a product is selected. If Not Me.ProductNameListBox.SelectedIndex < 0 Then Dim product As String = Me.ProductNameListBox.SelectedItem.Row.Item("ProductName") Dim company As String = Me.CompanyNameComboBox.Text ' Return the data row from the selected Product in the list box. rows = tbl.Select("[ProductName] = '" & product.Replace("'", "''") & "'") AddData(rows(0), company) Else MessageBox.Show("Please select a product.", "Actions Pane", MessageBoxButtons.OK) End If End Sub
The actions pane becomes visible after controls are added to it.
In Solution Explorer, right-click ThisDocument.vb or ThisDocument.cs, and then click View Code on the shortcut menu.
Create a new instance of the control at the top of the
ThisDocumentclass so that it looks like the following example.
Add code to the Startup event handler of
ThisDocumentso that it looks like the following example.
Now you can test your document to verify that the actions pane appears 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.
Press F5 to run your project.
Confirm that the actions pane is visible.
Select a company in the combo box and verify that the items in the Products list box change.
Select a product, click Insert on the actions pane, and verify that the product details are added to the table in Word.
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:
Binding data to controls in Excel. For more information, see Walkthrough: Binding Data to Controls on an Excel Actions Pane.
Deploying the project. For more information, see Deploying an Office Solution by Using ClickOnce.