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. 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.
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.
The first step is to create a Word Document 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.
The actions pane will send text to bookmarks in the document. To design the document, type some text to create a basic form.
Type the following text into your Word document:
March 21, 2008
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.
From the Word Controls tab of the Toolbox, drag a Bookmark control to your document.
The Add Bookmark Control dialog box appears.
Select the word Name, without selecting the paragraph mark, and click OK.
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.
In the Properties window, change the Name property of Bookmark1 to showName.
Select the word Address, without selecting the paragraph mark.
On the Insert tab of the Ribbon, in the Links group, click Bookmark.
In the Bookmark dialog box, type showAddress in the Bookmark Name box and click Add.
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.
Select the My Basic Actions Pane project in Solution Explorer.
On the Project menu, click Add New Item.
In the Add New Item dialog box, click Actions Pane Control, name the control InsertTextControl, and click Add.
If the actions pane control is not visible in the designer, double-click InsertTextControl.
From the Common Controls tab of the Toolbox, drag a Label control to the actions pane control.
Change the Text property of the Label control to Name.
Add a Textbox control to the actions pane control, and change the following properties.
Property Value Name getName Size 130, 20
Add a second Label control to the actions pane control, and change the Text property to Address.
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
Add a Button control to the actions pane control, and change the following properties.
Property Value Name addText Text Insert
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.
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
In C#, you must add an event handler for the button click. You can place this code in the
InsertTextControlconstructor after the call to
IntializeComponent. For information about creating event handlers, see How to: Create Event Handlers in Office Projects.
To show the actions pane, add the control you created to the control collection.
Create a new instance of the actions pane control in the
Add the following code to the Startup event handler of
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.
Press F5 to run your project.
Confirm that the actions pane is visible.
Type your name and address into the text boxes on the actions pane and click Insert.
Here are some tasks that might come next:
Creating an actions pane in Excel. For more information, see How to: Add an Actions Pane to Excel Workbooks.
Binding data to controls on an actions pane. For more information, see Walkthrough: Binding Data to Controls on a Word Actions Pane.