次の方法で共有


Walkthrough: Insert text into a document from an actions pane

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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. For more information, see Features available by Office application and project type.

This walkthrough illustrates the following tasks:

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

  • Display 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 Personalize the Visual Studio IDE.

Prerequisites

You need the following components to complete this walkthrough:

Create the project

The first step is to create a Word Document project.

To create a new project

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

Add 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

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

Add 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

Add 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 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;
    }
    
    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
    
  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 InitializeComponent. 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);
    }
    

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

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

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

Test 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