Creating an Actions Pane Solution

Usually, you place the code that adds the control to the actions pane in the Startup event handler of either ThisDocument or ThisWorkbook, depending on whether you're working in Word or Excel. This causes the code to add the control to the actions pane so that it is visible when you open the document. You can also add this code to other procedures. For example, you might want to display a control in the Document Actions task pane when a user moves the cursor into a particular area of the document.

You can access objects on the document from the user control on the actions pane by using the Globals class provided by VSTO. For example, if you write code in the Click event handler of a Button on the actions pane so that it adds text to the document, you can access objects in the document, such as the Paragraph object, by typing

Globals.ThisDocument.Paragraphs(1).Range.Text = _ "You clicked the button!"

The Globals class gives you access to objects on a worksheet or document from outside the ThisWorkbook, Sheetl, Sheet2, Sheet3, or ThisDocument class. In Excel, Globals gives you access to the workbook or the worksheets, and from there you can access any object on the worksheet. For example, if you want to reference a text box that exists on Sheet2 from the Sheetl class, you can write the following code:

Dim text as String = Globals.Sheet2.TextBox1.Text

It is important to understand that the actions pane is document-centric. You cannot have one actions pane that is globally available to every document that you have opened; each document must have a separate actions pane. Because there is only one task pane available for your application, you can display only one actions pane. However, you can display multiple actions pane controls, and you can hide and show controls to simulate various task panes, as discussed in "Creating Context-Sensitive Solutions" later in this chapter.

If you want to display a task pane in multiple documents or show multiple task panes in your solution, you can do so using VSTO 2005 SE and either Office 2003 or Office 2007. In Chapter 14 you'll learn more about creating add-ins for several Office applications using VSTO 2005 SE.

Word has a Styles and Formatting task pane where you can create and apply styles to your document. Excel doesn't have a Styles and Formatting task pane, and the interface for adding these styles is not intuitive. With VSTO, you can create your own custom Styles and Formatting task pane for Excel. The following sections explain how.

0 0

Post a comment