Visual Studio Tools for the Microsoft Office System
Walkthrough: Changing the Actions Pane According to User Context

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 how to add and remove actions pane controls on an actions pane in Microsoft Office Word in response to XMLNode events. As the user moves through the document, the corresponding actions pane controls become visible.

During this walkthrough, you will learn how to:

  • Create actions pane controls for the actions pane.

  • Add Windows Forms controls to actions pane controls.

  • Show the actions pane when the application opens.

  • Hide and show actions pane controls on the actions pane based on user actions in XML nodes.

  • 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.

Prerequisites

To complete this walkthrough, you will need:

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

  • Microsoft Office Word 2003.

Creating the Project

In this step, you will create a Word Document project using Visual Studio Tools for Office.

To create a new project

  • Create a Word Document project with the name My Dynamic 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 Dynamic Actions Pane project to Solution Explorer.

Creating a Schema to Map to the Document

In this step, you will create an XML schema, which you will map to your Word document later in this walkthrough.

To create the XML schema

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

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

    The Add New Item dialog box appears.

  3. Select XML Schema in the Templates pane.

  4. Name the schema ActionsPaneSample.xsd and click Add.

  5. On the View menu, select Open With.

  6. In the Open With dialog box, select XML Editor and click Open.

  7. Replace the text in the XML editor with the following text:

    <?xml version="1.0" encoding="utf-8" ?>
    <xs:schema targetNamespace="http://schemas.microsoft.com/vsto/samples" elementFormDefault="qualified"
        xmlns="http://schemas.microsoft.com/vsto/samples"
        xmlns:mstns="http://schemas.microsoft.com/vsto/samples"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Sample" type="SampleType"></xs:element>
      <xs:complexType name="SampleType">
        <xs:all>
          <xs:element name="Insert" type="InsertType" minOccurs="0" maxOccurs="1" />
          <xs:element name="Table" type="xs:string" minOccurs="0" maxOccurs="1" />
        </xs:all>
      </xs:complexType>
      <xs:complexType name="InsertType">
        <xs:sequence>
          <xs:element name="MemoTo" type="xs:string" minOccurs="1" maxOccurs="1"/>
          <xs:element name="MemoFrom" type="xs:string" minOccurs="1" maxOccurs="1"/>
          <xs:element name="MemoSubject" type="xs:string" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
  8. Save your project.

Adding Text and XML to the Document

In this step, you will create a memorandum, add text to the document, and map XML elements to portions of the document.

To add text to your document

  1. Right click ThisDocument in Solution Explorer, and select View Designer.

  2. Add the following text to your Word document:

    MEMORANDUM

    To:

    From:

    Subject:

    The following table lists the contact information you requested:

    Name

    Address

    City

    State

    Postal Code

    Nancy Davolio

    507 - 20th Ave. E., Apt. 2A

    Seattle

    WA

    98122

Next, you will map XML elements to the text within the memorandum. An XMLNode control is created for each XML element that you map. The XMLNode control is named by appending the parent and child element names to the word Node. For example, if you map the Insert element from the schema above, it is named SampleInsertNode. For more information on controls, see Host Items and Host Controls Overview.

To attach the schema to your document

  1. On the Tools menu, point to Microsoft Office Word Tools, and select Templates and Add-Ins.

    NoteNote

    If Microsoft Office Word Tools Commands is not available in the Tools menu, click the document to give it focus. For more information see, Office Menus in the Visual Studio Environment.

  2. In the Templates and Add-ins dialog box, click the XML Schema tab, and click Add Schema.

  3. Browse to the ActionsPaneSample.xsd schema you created earlier, which is located in your project directory, and click Open.

  4. Click OK in the Schema Settings dialog box.

  5. Click OK to close the Templates and Add-ins dialog box.

    The XML Structure task pane opens.

To map XML elements to your document

  1. Click the Sample element in the XML Structure task pane.

  2. Click Apply to Entire Document when prompted.

  3. Select the three lines of text that contains To, From, and Subject, and click the Insert element in the XML Structure task pane.

  4. Place your cursor after To:, press the TAB key twice, and click the MemoTo element to insert it.

  5. Place your cursor after From:, press the TAB key twice, and click the MemoFrom element.

  6. Place your cursor after Subject:, press the TAB key once, and click the MemoSubject element.

  7. Select the table so that the entire table is highlighted, and click the Table element to apply it.

    The XML tags appear within the table cells.

  8. Clear the Show XML tags in the document check box.

  9. Click XML Options at the bottom of the XML Structure task pane.

  10. Select the Ignore mixed content check box, and click OK.

  11. Close the XML Structure task pane.

Adding Controls to the Actions Pane

In this section, you will add three actions pane controls to the project and then add Windows Forms controls to each actions pane control. The first actions pane control contains three text boxes and a button to allow end users to enter data and add it to the document. The second actions pane control contains a button that opens the Table Properties dialog box in Word. The third actions pane control contains a checkbox to allow end users to show all controls on the actions pane, regardless of where the cursor is in the document.

To add an actions pane control

  1. Select the My Dynamic 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 AddTextControl, and click Add.

  4. Change the Size property of the actions pane control to 170, 135.

To add Windows Form controls to the first actions pane control

  1. From the Windows Forms tab of the Toolbox, drag a Label control to AddTextControl, and change the Text property to To:.

  2. Add a Textbox control to AddTextControl and change the following properties:

    Property Value

    Name

    toBox

    Size

    110, 20

  3. Add a second Label control to AddTextControl, and change the Text property to From:.

  4. Add a second Textbox control to AddTextControl and change the following properties:

    Property Value

    Name

    fromBox

    Size

    110, 20

  5. Add a third Label control to AddTextControl under the previous text box, and change the Text property to Subject:.

  6. Add a third Textbox control to AddTextControl next to the previous label, and change the following properties:

    Property Value

    Name

    subjectBox

    Multiline

    True

    Size

    110, 40

  7. Resize the subjectBox text box to allow multiple lines.

  8. Add a Button control to AddTextControl and change the following properties:

    Property Value

    Name

    insertText

    Text

    Insert

To create the second actions pane control

  1. Add a second actions pane control to your project and name it ShowPropertiesControl.

  2. Change the Size property of the actions pane control to 170, 50.

  3. From the Windows Forms tab of the Toolbox, drag a Button control to ShowPropertiesControl and change the following properties:

    Property Value

    Name

    tableProperties

    Text

    Table Properties

    Enabled

    False

    Size

    100, 23

  4. For C#, change the Modifiers property of the button to Internal.

To create the third actions pane control

  1. Add a third actions pane control to your project and name it ShowAllControl.

  2. Change the Size property of the actions pane control to 170, 75.

  3. From the Windows Forms tab of the Toolbox, add a GroupBox control to ShowAllControl and change the Text property to My Actions Pane.

  4. Add a CheckBox control to ShowAllControl on top of the group box and change the following properties:

    Property Value

    Name

    showCheck

    Text

    Show All Controls

  5. For C#, change the Modifiers property of the check box to Internal.

Adding Code to Respond to User Context

In this section, you will write code to show the actions pane, and add and remove actions pane controls on the actions pane according to the cursor location within the document. You will add code to the event handlers of the XMLNode controls.

To add code to XMLNode control events

  1. In Solution Explorer, right-click ThisDocument.vb or ThisDocument.cs and select View Code.

  2. Create a variable reference for each actions pane control in the declarations section of ThisDocument, as follows:

    Visual Basic
    Dim addText As New AddTextControl
    Dim showProperties As New ShowPropertiesControl
    Dim showAll As New ShowAllControl
    
    Friend WithEvents showCheck As System.Windows.Forms.CheckBox
    C#
    private AddTextControl addText = new AddTextControl();
    private ShowPropertiesControl showProperties = new ShowPropertiesControl();
    private ShowAllControl showAll = new ShowAllControl();
  3. In the Startup event handler of ThisDocument, write the following code to add the ShowAllControl control and show the actions pane:

    Visual Basic
    Me.ActionsPane.Controls.Add(showAll)
    
    showCheck = showAll.showCheck
    C#
    this.ActionsPane.Controls.Add(showAll);
    NoteNote

    The order in which you add controls to the actions pane determines their placement. If you have a control that will always be visible at the top of the actions pane, you should add it first. Order is also dependent on the StackOrder property of the actions pane. For more information, see How to: Manage Control Layout on Actions Panes.

  4. Add the following code in the ContextEnter event handler of the SampleInsertNode control:

    Visual Basic
    Private Sub SampleInsertNode_ContextEnter(ByVal sender As Object, _
        ByVal e As Microsoft.Office.Tools.Word.ContextChangeEventArgs) _
        Handles SampleInsertNode.ContextEnter
    
        If showAll.showCheck.Checked = False Then
            Me.ActionsPane.Controls.Add(addText)
            Me.ActionsPane.Controls.Remove(showProperties)
        End If
    End Sub
    C#
    private void SampleInsertNode_ContextEnter(object sender, 
        Microsoft.Office.Tools.Word.ContextChangeEventArgs e)
    {
        if (showAll.showCheck.Checked == false)
        {
            this.ActionsPane.Controls.Add(addText);
            this.ActionsPane.Controls.Remove(showProperties);
        }
    }
  5. Add the following code to the ContextLeave event handler of the SampleInsertNode control to hide the control once the user has moved the cursor outside of this XML node:

    Visual Basic
    Private Sub SampleInsertNode_ContextLeave(ByVal sender As Object, _
        ByVal e As Microsoft.Office.Tools.Word.ContextChangeEventArgs) _
        Handles SampleInsertNode.ContextLeave
    
        If showAll.showCheck.Checked = False Then
            Me.ActionsPane.Controls.Remove(addText)
        End If
    End Sub
    C#
    private void SampleInsertNode_ContextLeave(object sender,
        Microsoft.Office.Tools.Word.ContextChangeEventArgs e)
    {
        if (showAll.showCheck.Checked == false)
        {
            this.ActionsPane.Controls.Remove(addText);
        }
    }
  6. Add the following code in the ContextEnter event handler of the SampleTableNode control:

    Visual Basic
    Private Sub SampleTableNode_ContextEnter(ByVal sender As Object, _
        ByVal e As Microsoft.Office.Tools.Word.ContextChangeEventArgs) _
        Handles SampleTableNode.ContextEnter
    
        If showAll.showCheck.Checked = False Then
            Me.ActionsPane.Controls.Remove(addText)
            Me.ActionsPane.Controls.Add(showProperties)
        End If
    
        showProperties.tableProperties.Enabled = True
    End Sub
    C#
    private void SampleTableNode_ContextEnter(object sender,
        Microsoft.Office.Tools.Word.ContextChangeEventArgs e)
    {
        if (showAll.showCheck.Checked == false)
        {
            this.ActionsPane.Controls.Remove(addText);
            this.ActionsPane.Controls.Add(showProperties);
        }
        showProperties.tableProperties.Enabled = true;
    }
  7. Add the following code in the ContextLeave event handler of the SampleTableNode control to hide the control once the user has moved the cursor outside of this XML node:

    Visual Basic
    Private Sub SampleTableNode_ContextLeave(ByVal sender As Object, _
        ByVal e As Microsoft.Office.Tools.Word.ContextChangeEventArgs) _
        Handles SampleTableNode.ContextLeave
    
        If showAll.showCheck.Checked = False Then
            Me.ActionsPane.Controls.Remove(showProperties)
        End If
    
        showProperties.tableProperties.Enabled = False
    End Sub
    C#
    private void SampleTableNode_ContextLeave(object sender,
        Microsoft.Office.Tools.Word.ContextChangeEventArgs e)
    {
        if (showAll.showCheck.Checked == false)
        {
            this.ActionsPane.Controls.Remove(showProperties);
        }
        showProperties.tableProperties.Enabled = false;
    }
  8. In C#, you must add event handlers for the XMLNode controls. You can place this code in the Startup event handler of ThisDocument as shown below. For information on creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    C#
    this.SampleInsertNode.ContextEnter += new Microsoft.Office.Tools.Word.
        ContextChangeEventHandler(SampleInsertNode_ContextEnter);
    
    this.SampleInsertNode.ContextLeave += new Microsoft.Office.Tools.Word.
        ContextChangeEventHandler(SampleInsertNode_ContextLeave);
    
    this.SampleTableNode.ContextEnter += new Microsoft.Office.Tools.Word.
        ContextChangeEventHandler(SampleTableNode_ContextEnter);
    
    this.SampleTableNode.ContextLeave += new Microsoft.Office.Tools.Word.
        ContextChangeEventHandler(SampleTableNode_ContextLeave);

Checkpoint

Save your project and verify that the actions pane controls change as you move your cursor through the document.

To test your project

  1. Press F5 to run your project.

  2. Move your insertion point to the word To and verify that the addText actions pane control is visible on the actions pane.

  3. Move your insertion point to inside the table and verify that the showProperties actions pane control is visible on the actions pane.

  4. Move your insertion point to the word MEMORANDUM and verify that neither the addText or showProperties actions pane controls is visible on the actions pane.

Adding Code to Respond to Control Events on the Actions Pane

In this section, you will write code to show all controls, insert text and show the Table Properties dialog box.

To show all controls on the actions pane

  1. Add the following code to ThisDocument:

    Visual Basic
    Private Sub showCheck_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles showCheck.CheckStateChanged
    
        If showAll.showCheck.Checked = True Then
            Me.ActionsPane.Controls.Add(addText)
            Me.ActionsPane.Controls.Add(showProperties)
        Else
            Me.ActionsPane.Controls.Remove(addText)
            Me.ActionsPane.Controls.Remove(showProperties)
        End If
    End Sub
    C#
    private void showCheck_CheckStateChanged(object sender, EventArgs e)
    {
        if (showAll.showCheck.Checked)
        {
            this.ActionsPane.Controls.Add(addText);
            this.ActionsPane.Controls.Add(showProperties);
        }
        else
        {
            this.ActionsPane.Controls.Remove(addText);
            this.ActionsPane.Controls.Remove(showProperties);
        }
    }
  2. In C#, you must add an event handler for the checkbox control. You can place this code in the Startup event handler of ThisDocument as shown below. For information on creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    C#
    showAll.showCheck.CheckStateChanged += new EventHandler(showCheck_CheckStateChanged);

To insert text when the Insert button is clicked

  1. In Solution Explorer, right-click AddTextControl.vb or AddTextControl.cs and select View Code.

  2. In the Click event handler of the insertText button, write the following code:

    Visual Basic
    Private Sub insertText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles insertText.Click
    
        With Globals.ThisDocument
            .InsertMemoFromNode.Text = Me.fromBox.Text
            .InsertMemoToNode.Text = Me.toBox.Text
            .InsertMemoSubjectNode.Text = Me.subjectBox.Text
        End With
    
        ' Clear the text boxes.
        Me.fromBox.Text = ""
        Me.toBox.Text = ""
        Me.subjectBox.Text = ""
    End Sub
    C#
    private void insertText_Click(object sender, System.EventArgs e)
    {
        Globals.ThisDocument.InsertMemoFromNode.Text = this.fromBox.Text;
        Globals.ThisDocument.InsertMemoToNode.Text = this.toBox.Text;
        Globals.ThisDocument.InsertMemoSubjectNode.Text = this.subjectBox.Text;
    
        // Clear the text boxes.
        this.fromBox.Text = "";
        this.toBox.Text = "";
        this.subjectBox.Text = "";
    }
  3. In C#, you must add an event handler for the button. You can place this code in the AddTextControl constructor after the call to InitializeComponent, as shown below. For information on creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    C#
    public AddTextControl()
    {
        InitializeComponent();
        this.insertText.Click += new System.EventHandler(this.insertText_Click);
    }

To show the Table Properties dialog box

  1. In Solution Explorer, right-click ShowPropertiesControl.vb or ShowPropertiesControl.cs and select View Code.

  2. Add the following code to the Click event handler of the tableProperties button:

    Visual Basic
    Private Sub tableProperties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles tableProperties.Click
    
        Globals.ThisDocument.Application.Dialogs( _
            Word.WdWordDialog.wdDialogTableProperties).Show()
    End Sub
    C#
    private void tableProperties_Click(object sender, System.EventArgs e)
    {
        object timeout = 0;
    
        Globals.ThisDocument.Application.Dialogs[
            Microsoft.Office.Interop.Word.WdWordDialog.wdDialogTableProperties]
                .Show(ref timeout);
    }
  3. In C#, you must add an event handler for the button. You can place this code in the ShowPropertiesControl constructor after the call to InitializeComponent, as shown below. For information on creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    C#
    public ShowPropertiesControl()
    {
        InitializeComponent();
        this.tableProperties.Click += new System.EventHandler(this.tableProperties_Click);
    }
Testing the Application

Now you can further test your document to verify the functionality of the actions pane controls.

To test your document

  1. Press F5 to run your project.

  2. Confirm that the actions pane is visible.

  3. Move your cursor to the section of the document that contains the To, From, and Subject lines, and confirm that the addText controls appear on the actions pane.

  4. Enter text in the To, From, and Subject text boxes on the actions pane, and click Insert.

  5. Move your cursor to the table and confirm that the showProperties control appears on the actions pane.

  6. Click the Table Properties button and confirm that the Table Properties dialog box opens.

  7. Click the Show All check box and confirm that all controls on the actions pane are visible.

  8. Move your cursor outside of the table and confirm that the Table Properties button becomes disabled.

Next Steps

This walkthrough shows the basics of changing the actions pane based on user context. Here are some tasks that might come next:

See Also

Tasks

How to: Add an Actions Pane to Word Documents
How to: Add an Actions Pane to Excel Workbooks
How to: Manage Control Layout on Actions Panes
How to: Create an ASP.NET User Control

Concepts

Actions Pane Overview
XMLNodes Control

Tags :


Page view tracker