Walkthrough: Inserting Text into a Document from an Actions Pane
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.
Microsoft Office version
For more information, see Features Available by Application and Project Type.
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.
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.
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 Visual Studio Settings.
You need the following components to complete this walkthrough:
Visual Studio Tools for Office (an optional component of Visual Studio 2008 Professional and Visual Studio Team System).
Microsoft Office Word 2003 or Microsoft Office Word 2007.
Visual Studio Tools for Office is installed by default with the listed versions of Visual Studio. To check whether it is installed, see Installing Visual Studio Tools for Office.
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 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.
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
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
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.
Perform one of the following steps:
For Word 2003: On the Insert menu, click Bookmark.
For Word 2007: 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.
To add an 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.
To add Windows Form controls to the actions pane control
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.
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.
Add a Button control to the actions pane control, and change the following properties.
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 Visual Studio Tools for Office Projects.
To insert text from the actions pane in a bookmark in the document
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 InsertTextControl constructor after the call to IntializeComponent. For information about creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.
To show the actions pane, add the control you created to the control collection.
To show the actions pane
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
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.
Showing and hiding controls on the actions pane. For more information, see Walkthrough: Changing the Actions Pane According to User Context.