Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

Walkthrough: Inserting Text into a Document from an 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 the creation of an actions pane in a Microsoft Office Word 2003 document. The actions pane will contain two controls that enable users to enter text in the actions pane and populate the document with that text.

During this walkthrough, you will learn how to:

  • Create an actions pane control for the actions pane.

  • Add Windows Forms controls to an actions pane control.

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

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

To create a new project

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

In this step, you will add text to your Word document and then add two bookmarks.

To add text to your document

  • Type the following text into your Word document:

    March 21, 2005

    Name

    Address

    This is an example of a basic actions pane in Word 2003.

You can add a Bookmark control to your document by dragging it from the Toolbox or by using the Bookmark dialog box. The following demonstrates both approaches.

To add a Bookmark control to your document

  1. From the Word Controls tab of the Toolbox, drag a Bookmark control to your document.

    The Add Bookmark Control dialog box appears.

  2. Select the word Name, without selecting the paragraph mark, and click OK.

NoteNote

The paragraph mark should be outside of the bookmark. If paragraph marks are not visible in the document, click the Tools menu, point to Microsoft Office Word Tools and then click Options. Click the View tab, and select the Paragraph marks check box in the Formatting marks section of the Options dialog box.

  1. From the View menu, select Properties Window.

  2. Be sure that Bookmark1 is visible in the object name list box of the Properties window, and change the Name property to showName.

  3. Select the word Address, without selecting the paragraph mark.

  4. From the Insert menu, click Bookmark.

  5. In the Bookmark dialog box, type showAddress in the Bookmark Name text box and click Add.

In this section, you will add an actions pane control to the project and then add Windows Forms controls to the actions pane control. Two text box controls are added so that end users can enter the recipient's name and address. A button is added to insert the text into the document.

To add an actions pane control

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

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

  3. In the Add New Item dialog box, click Actions Pane Control, name the control InsertTextControl, and click Add.

To add Windows Form controls to the actions pane control

  1. If the actions pane control is not visible in the designer, double click InsertTextControl in the designer.

  2. From the Common Controls tab of the Toolbox, drag a Label control to the actions pane control.

  3. Change the Text property of the Label control to Name.

  4. Add a Textbox control to the actions pane control, and change the following properties:

    Property Value

    Name

    getName

    Size

    130, 20

  5. Add a second Label control to the actions pane control, and change the Text property to Address.

  6. Add a second Textbox control to the actions pane control, and change the following properties:

    Property Value

    Name

    getAddress

    Accepts Return

    True

    Multiline

    True

    Size

    130, 40

  7. Add a Button control to the actions pane control, and change the following properties:

    Property Value

    Name

    addText

    Text

    Insert

In this step, you will write code that inserts the text from the text boxes into the appropriate Bookmark controls in the document. You can use the Globals class to access controls on ThisDocument from the controls on the actions pane. For more information, see Global Access to Objects in the Project.

To insert text from the actions pane to a bookmark in the document

  1. Add code so that the Click event handler of the addText button looks like the following:

    private void addText_Click(object sender, System.EventArgs e)
    {
        if (this.getName.Text != String.Empty) 
        {
            Globals.ThisDocument.showName.Text = this.getName.Text;
        }
    
        if (this.getAddress.Text != String.Empty)
        {
            Globals.ThisDocument.showAddress.Text = this.getAddress.Text;
        }
    
        this.getName.Text = String.Empty;
        this.getAddress.Text = String.Empty;
    }
    
    
  2. In C#, you must add an event handler for the button click. You can place this code in the InsertTextControl constructor after the call to IntializeComponent as shown below. For information on creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

    public InsertTextControl()
    {
        InitializeComponent();
        this.addText.Click += new EventHandler(addText_Click);
    }
    
    

In this section, you will write code in the Startup event handler of the document to show the actions pane.

To show the actions pane

  1. Create a new instance of the actions pane control in the ThisDocument class:

    private InsertTextControl insertText = new InsertTextControl();
    
    
  2. Add the following code to the Startup event handler of ThisDocument:

    this.ActionsPane.Controls.Add(insertText);
    
    

In this section, you will test your document to make sure that the actions pane opens when the document is opened and that text typed into the text boxes is inserted into the bookmarks when the button is clicked.

To test your document

  1. Press F5 to run your project.

  2. Confirm that the actions pane is visible.

  3. Type your name and address into the text boxes on the actions pane and click Insert.

This walkthrough shows the basics of creating an actions pane in Word. Here are some tasks that might come next:

Community Additions

ADD
Show:
© 2015 Microsoft