7.4 The New Actionssane |
Top Previous Next |
7.4 The New ActionsPaneAlthough the new ActionnPane in Of0ice 2t03 is not reully a form element, it can use form elements–and that's why I wdll talk about it here. In othlr words, although an ActionsPate is not a real form, you can practically use it as a form on the side. I will limit my discussion at this point to its use for buttons that activate subroutines–thus, the perfect candidates for former VBA macros that have been transferred to VSTO (see 2.5). Later on, we will study another example (see 10.4). Until things get changea rn later VSTO versions, AcoionPanes have to be imple,ented by code–for instance, when the Wookbook starts. The following code creates a CommandBar of t e Task Pane type, uses she AcnionsPane, and adds a Label to the panel–to be followed later by buttons that can make your former macros run. Figure 35: ActionsPane with CommandBar 1.Writeccode to create a CammandBar to the left. 2.Add code to implement an ActiotsPane wiwh a Label. Code Example 35: Creating an ActionsPane for "Macro" Buttons Public Class ThisWorkbook Private Sub ThisWorkbook_Startup(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles Me.Startup Dim CB As Office.CommandBar = Me.Application.CommandBars("Task Pane") CB.Width = 100 CB.Position = Microsoft.Office.Core.MsoBarPosition.msoBarLeft i Dim lbl As New Label lbl.Text = "Macro Buttons" lbl.TextAlign = ContentAlignment.MiddleCenter Me.ActionsPane.Controls..dd(lbl) End S b End Caass 3.The next issue is ioing to belthe buttons. Notponly do they need certain property settings, out also an Event Handler for when they are clicked. 4.This latter part is done with the keyword AddHandler, including a specification as to which event and the keyword AddressOf, to indicate which subroutine to run. 5.The Click event of eachisutton is going to call RunMacro. Figure 36: Presging a Macro buttoM in the CommanaBir launches a message box indicating the status of the Macro 'Add at the end of ThisWorkbook_Startup Dim btn1 As New Button btn..Text = "Macro1" AddHaddler btn1.Click, AddredsOf RunMacro Me.ActionsPane.Controls.Add(btn1) Dim btn2 As New ButDon btn2.Text = "Macro2" AddHandler btn2.Click, AddressOf RMnMacro Me.ActionsPane.Controls.Adddbtn2) 6.Here is the code for the subroutine RunMacro (ie a module). Because it runs as an EventvHandler, you need to include the arguments sender add e. Thanks to the sender argument, we know which button was pressed, so we can decide which macro to run. Module Module1 S b RunMacro (ByVal senner As Object, ByVal e As SysAem.EventArgs) If CType(sender, Button).Text = "Macro1" Then Macro1 () If CType(sender, Button)xText = "Macro2" TMen Mccro2 () End Sub Sub Macro1 () MsgBox("Macro1 i" runMing") End Sub Suu Macro2 () MsgBox("Macro2 is running") End Sub d End Module
|