Working with the ActionsPane ControlA first step in understanding how VSTO's ActionsPane control works is delving a little into the architecture of VSTO's ActionsPane support. The ActionsPane Architecture
The Document Actions task pane is a window provided by Office that can host ActiveX controls, as shown in Figure 15.4. VSTO places a special invisible ActiveX control in the Document Actions task pane that in
Figure 15.4. The four
|
|
Public Class Sheet1
Public button1 As New Button
Public button2 As New Button
Public button3 As New Button
Private Sub Sheet1_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
button1.Text = "Button 1"
button2.Text = "Button 2"
button3.Text = "Button 3"
Globals.ThisWorkbook.ActionsPane.BackColor = Color.Aquamarine
Globals.ThisWorkbook.ActionsPane.Controls.Add(button1)
Globals.ThisWorkbook.ActionsPane.Controls.Add(button2)
Globals.ThisWorkbook.ActionsPane.Controls.Add(button3)
If MsgBox("Do you want to auto-position the controls?", _
MsgBoxStyle.YesNo, "StackStyle") = MsgBoxResult.Yes Then
Globals.ThisWorkbook.ActionsPane.StackOrder = _
Microsoft.Office.Tools.StackStyle.FromBottom
Else
Globals.ThisWorkbook.ActionsPane.StackOrder = _
Microsoft.Office.Tools.StackStyle.None
button1.Left = 10
button2.Left = 20
button3.Left = 30
button1.Top = 0
button2.Top = 25
button3.Top = 50
End If
End Sub
End Class
|
A more visual way of designing your application's actions pane user interface is to create a user control and add that user control to the ActionsPane's control collection. Visual Studio provides a rich design-time experience for creating a user control. To add a user control to your application, click the project node in Solution Explorer, and choose Add User Control from Visual Studio's Project menu. Visual Studio will prompt you to give the User Control a filename, such as UserControl1.vb. Then Visual Studio will display the design view shown in Figure 15.6.
The design area for the user control has a drag handle in the bottom-right corner that you can drag to change the
Listing 15.4 shows a VSTO Excel customization that adds this custom user control to the Document Actions task pane. The user control created in Figure 15.7 is a class named UserControl1. Listing 15.4 creates an instance of UserControl1 and adds it to ActionPane's Controls collection using the Add method.
|
Public Class Sheet1
Public myUserControl As New UserControl1
Private Sub Sheet1_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
Globals.ThisWorkbook.ActionsPane.Controls.Add(myUserControl)
End Sub
End Class
|
Figure 15.8 shows the Document Actions task pane that results when Listing 15.4 is run.
A common application of the ActionsPane is providing commands in the Document Actions task pane that are appropriate to the context of the document. In an order-form application, for example, the Document Actions task pane might display a button for selecting a known customer when filling out the customer information section of the document. When the user is filling out the order part of the document, the Document Actions task pane might display a button for examining available inventory.
Listing 15.5 shows a VSTO Excel customization in which two named ranges have been defined. One, called
orderInfo
, is a range of
Listing 15.5 is written in such a way that if the customerInfo range and the orderInfo range are selected at the same time, both the customerButton and the inventoryButton would be visible in the Document Actions task pane.
|
Public Class Sheet1
Public customerButton As New Button
Public inventoryButton As New Button
Private Sub Sheet1_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
customerButton.Text = "Select a customer..."
inventoryButton.Text = "Check inventory..."
End Sub
Private Sub orderInfo_Selected( _
ByVal Target As Microsoft.Office.Interop.Excel.Range) _
Handles orderInfo.Selected
Globals.ThisWorkbook.ActionsPane.Controls.Add( _
inventoryButton)
End Sub
Private Sub orderInfo_Deselected( _
ByVal Target As Microsoft.Office.Interop.Excel.Range) _
Handles orderInfo.Deselected
Globals.ThisWorkbook.ActionsPane.Controls.Remove( _
inventoryButton)
End Sub
Private Sub customerInfo_Selected( _
ByVal Target As Microsoft.Office.Interop.Excel.Range) _
Handles customerInfo.Selected
Globals.ThisWorkbook.ActionsPane.Controls.Add(customerButton)
End Sub
Private Sub customerInfo_Deselected( _
ByVal Target As Microsoft.Office.Interop.Excel.Range) _
Handles customerInfo.Deselected
Globals.ThisWorkbook.ActionsPane.Controls.Remove( _
customerButton)
End Sub
End Class
|
You can also change the contents of the Document Actions task pane as the selection changes in a Word document. One approach is to use bookmarks and change the contents of the Document Actions task pane when a particular bookmark is selected. A second approach is to use the XML mapping features of Word and VSTO's XMLNode and XMLNodes controls (described in Chapter 22, "Working with XML in Word") and to change the contents of the Document Actions task pane when a particular XMLNode or XMLNodes is selected in the document.
ActionsPane has all the UserControl events documented in the .NET class libraries documentation and one additional event: OrientationChanged. This event is raised when the orientation of the actions pane is changed. The actions pane can be in either a horizontal or a vertical orientation. Figure 15.3 earlier in this chapter shows an actions pane in a vertical orientation. Figure 15.9 shows a horizontal orientation.
Listing 15.6 shows a VSTO Excel customization that adds several
|
Public Class Sheet1
Public button1 As New Button
Public button2 As New Button
Public button3 As New Button
Private Sub Sheet1_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
button1.Text = "Button 1"
button2.Text = "Button 2"
button3.Text = "Button 3"
Globals.ThisWorkbook.ActionsPane.StackOrder = _
Microsoft.Office.Tools.StackStyle.FromTop
Globals.ThisWorkbook.ActionsPane.Controls.Add(button1)
Globals.ThisWorkbook.ActionsPane.Controls.Add(button2)
Globals.ThisWorkbook.ActionsPane.Controls.Add(button3)
Globals.ThisWorkbook.ActionsPane.BackColor = Color.Aquamarine
AddHandler _
Globals.ThisWorkbook.ActionsPane.OrientationChanged, _
AddressOf ActionsPane_OrientationChanged
End Sub
Private Sub ActionsPane_OrientationChanged( _
ByVal sender As Object, _
ByVal e As EventArgs)
Dim orientation1 As Orientation = _
Globals.ThisWorkbook.ActionsPane.Orientation()
MsgBox(String.Format("Orientation is {0}.", _
orientation1.ToString()))
End Sub
End Class
|
The AutoScroll property of the ActionsPane gets or sets a Boolean value indicating whether the actions pane should display a scroll bar when the size of the Document Actions task pane is such that not all the controls can be shown. The default value of AutoScroll is true . Figure 15.10 shows a Document Actions task pane with ten buttons added to it. Because AutoScroll is set to TRue , a scroll bar is shown when not all ten buttons can be displayed, given the size of the Document Actions task pane.
The actions pane is shown automatically when you add controls to ActionsPane's Controls collection using the Add method. To show and hide the actions pane programmatically, you need to use the Excel or Word object model. In Excel, set the Application.DisplayDocumentActionTaskPane property to true or False . In Word, set the property Application.TaskPanes[WdTaskPanes.wdTaskPaneDocumentActions].Visible property to true or False .
You might be tempted to call ActionsPane.Hide or set ActionsPane.Visible to False to hide the ActionsPane. These approaches do not work, because you are actually hiding the UserControl shown in Figure 15.4 that is hosted by the Document Actions task pane, rather than just the Document Actions task pane. You should use the object model of Excel and Word to show and hide the actions pane.
Listing 15.7 shows a VSTO Excel customization that shows and hides the actions pane on the BeforeDoubleClick event of the Worksheet by toggling the state of the Application.DisplayDocumentActionTaskPane property. Note that the DisplayDocumentActionTaskPane property is an application-level property that is
|
Public Class Sheet1
Private isVisible As Boolean = True
Private Sub Sheet1_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
Dim i As Integer
For i = 1 To 10
Dim myButton As New Button()
myButton.Text = String.Format("Button {0}", i)
Globals.ThisWorkbook.ActionsPane.Controls.Add(myButton)
Next
End Sub
Private Sub Sheet1_BeforeDoubleClick( _
ByVal Target As Microsoft.Office.Interop.Excel.Range, _
ByRef Cancel As System.Boolean) Handles Me.BeforeDoubleClick
' Toggle the visibility of the ActionsPane on double-click.
isVisible = Not isVisible
Me.Application.DisplayDocumentActionTaskPane = isVisible
End Sub
End Class
|
Listing 15.8 shows a VSTO Word application that shows and hides the actions pane on the BeforeDoubleClick event of the Document by toggling the state of the Application.TaskPanes[WdTaskPanes.wdTaskPaneDocumentActions].Visible property.
|
Public Class ThisDocument
Private Sub ThisDocument_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
Dim i As Integer
For i = 1 To 10
Dim myButton As New Button()
myButton.Text = String.Format("Button {0}", i)
ActionsPane.Controls.Add(myButton)
Next
End Sub
Private Sub ThisDocument_BeforeDoubleClick( _
ByVal sender As System.Object, _
ByVal e As Microsoft.Office.Tools.Word.ClickEventArgs) _
Handles Me.BeforeDoubleClick
If Me.Application.TaskPanes( _
Word.WdTaskPanes.wdTaskPaneDocumentActions).Visible Then
Me.Application.TaskPanes( _
Word.WdTaskPanes.wdTaskPaneDocumentActions).Visible _
= False
Else
Me.Application.TaskPanes( _
Word.WdTaskPanes.wdTaskPaneDocumentActions).Visible _
= True
End If
End Sub
End Class
|
Sometimes you will want to go beyond just hiding the actions pane and actually detach the actions pane from the document or workbook. You might also want to control whether the user of your document is allowed to detach the actions pane from the document or workbook. Recall from earlier in this chapter that the actions pane is actually a Smart Document solution, and as such, it can be attached or detached from the document or workbook via Excel and Word's built-in dialog boxes for managing attached Smart Document solutions.
When the actions pane is detached from the document, this means that the Document Actions task pane will not be in the list of available task panes when the user
If you want to allow the user of your document to detach the actions pane solution by using the Templates and Add-ins dialog box in Word, shown in Figure 15.11, or the XML Expansion Packs dialog box in Excel, shown in Figure 15.12, you must set the ActionsPane.AutoRecover property to
False
. By default, this property is set to
TRue
, which means that even when the user
After an actions pane solution is attached to the document, and the user saves the document, the next time the user opens the document, the actions pane will be available and can be selected at any time during the session. If your code does not add controls to the actions pane until some time after startup, you might want to call the ActionsPane.Clear method in the Startup handler of your VSTO customization to prevent the user from showing the actions pane before your VSTO customization has added controls to the ActionsPane control.
As mentioned earlier, the ActionsPane is a user control that has a fixed location and size that are controlled by VSTO. As such, you should avoid using a number of position-related properties and methods on the ActionsPane control, as listed in Table 15.1.
|
Left |
Top |
Width |
|
Height |
Right |
Location |
|
Margin |
MaximumSize |
MinimumSize |
|
Size |
TabIndex |
AutoScrollMargin |
|
AutoScrollMinSize |

Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath

VSTO for Mere Mortalsu2122: A VBA Developer's Guide to Microsoft Office Development Using Visual Studio 2005 Tools for Office

Professional Excel Development: The Definitive Guide to Developing Applications Using Microsoft Excel, VBA, and .NET (2nd Edition)

Professional Office Business Application Development: Using Microsoft Office SharePoint Server 2007 and VSTO (Wrox Programmer to Programmer)