Condividi tramite


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

To add an actions pane to a Microsoft Office Word document or a Microsoft Excel workbook, first create a Windows Forms user control. Then, add the user control to the Controls property of the ThisDocument.ActionsPane field (Word) or ThisWorkbook.ActionsPane field (Excel) in your project.

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

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.

Creating the User Control

The following procedure shows how to create user control in a Word or Excel project. It also adds a button to the user control that writes text to the document or workbook when it is clicked.

To create the user control

  1. Open your Word or Excel document-level project in Visual Studio.

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

  3. In the Add New Item dialog box, select Actions Pane Control, name it HelloControl, and click Add.

    Note

    You can alternatively add a User Control item to your project. The classes generated by the Actions Pane Control and User Control items are functionally equivalent.

  4. From the Windows Forms tab of the Toolbox, drag a Button control onto the control.

    Note

    If the control is not visible in the designer, double click HelloControl in Solution Explorer.

  5. Add the code to the Click event handler of the button. The following example shows code for a Microsoft Office Word document.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
    
        Globals.ThisDocument.Paragraphs(1).Range.Text = "Hello world!" 
    End Sub
    
    private void button1_Click(object sender, System.EventArgs e)
    {
        Globals.ThisDocument.Paragraphs[1].Range.Text = "Hello World!";
    }
    
  6. In C#, you must add an event handler for the button click. You can place this code in the HelloControl constructor after the call to IntializeComponent.

    For information about how to create event handlers, see How to: Create Event Handlers in Office Projects.

    public HelloControl()
    {
        InitializeComponent();
        this.button1.Click += new EventHandler(this.button1_Click);
    }
    

Adding the User Control to the Actions Pane

To show the actions pane, add the user control to the Controls property of the ThisDocument.ActionsPane field (Word) or ThisWorkbook.ActionsPane field (Excel).

To add the user control to the actions pane

  1. Add the following code to the ThisDocument or ThisWorkbook class as a class-level declaration (do not add this code to a method).

    Dim hello As New HelloControl
    
    private HelloControl hello = new HelloControl();
    
  2. Add the following code to the ThisDocument_Startup event handler of the ThisDocument class or the ThisWorkbook_Startup event handler of the ThisWorkbook class.

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

See Also

Tasks

Walkthrough: Inserting Text into a Document from an Actions Pane

How to: Manage Control Layout on Actions Panes

Walkthrough: Inserting Text into a Document from an Actions Pane

Other Resources

Actions Pane Overview