Condividi tramite


Walkthrough: Inserting Text into a Document from an Actions Pane

This walkthrough demonstrates how to create an actions pane in a Microsoft Office Word document. The actions pane contains two controls that collect input and then send the text to the document.

Applies to: The information in this topic applies to document-level projects for Word 2013 and Word 2010. For more information, see Features Available by Office Application and Project Type.

This walkthrough illustrates the following tasks:

  • Designing an interface by using Windows Forms controls on an actions pane control.

  • Displaying the actions pane when the application opens.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

Prerequisites

You need the following components to complete this walkthrough:

Creating the Project

The first step is to create a Word Document project.

To create a new project

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

Adding Text and Bookmarks to the Document

The actions pane will send text to bookmarks in the document. To design the document, type some text to create a basic form.

To add text to your document

  • Type the following text into your Word document:

    March 21, 2008

    Name

    Address

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

You can add a Bookmark control to your document by dragging it from the Toolbox in Visual Studio or by using the Bookmark dialog box in Word.

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.

    Note

    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.

  3. In the Properties window, change the Name property of Bookmark1 to showName.

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

  5. On the Insert tab of the Ribbon, in the Links group, click Bookmark.

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

Adding Controls to the Actions Pane

To design the actions pane interface, add an actions pane control to the project and then add Windows Forms controls to the actions pane control.

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.

  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

Adding Code to Insert Text into the Document

In the actions pane, 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 the document from the controls on the actions pane. For more information, see Global Access to Objects in Office Projects.

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

  1. Add the following code to the Click event handler of the addText button.

    Private Sub addText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles addText.Click
    
        If Me.getName.Text <> String.Empty Then
            Globals.ThisDocument.showName.Text = Me.getName.Text
        End If 
    
        If Me.getAddress.Text <> String.Empty Then
            Globals.ThisDocument.showAddress.Text = Me.getAddress.Text
        End If 
    
        Me.getName.Text = String.Empty
        Me.getAddress.Text = String.Empty
    End Sub
    
    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. For information about creating event handlers, see How to: Create Event Handlers in Office Projects.

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

Adding Code to Show the Actions Pane

To show the actions pane, add the control you created to the control collection.

To show the actions pane

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

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

    Me.ActionsPane.Controls.Add(insertText)
    
    this.ActionsPane.Controls.Add(insertText);
    

Testing the Application

Test your document to verify 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.

Next Steps

Here are some tasks that might come next:

See Also

Tasks

How to: Add an Actions Pane to Word Documents or Excel Workbooks

How to: Manage Control Layout on Actions Panes

Concepts

Bookmark Control

Other Resources

Actions Pane Overview

How to: Add an Actions Pane to Excel Workbooks