2.5 What About Your Macros?

Top  Previous  Next

prev

next

 

2.5 What About Your Macros?

In VBA, you could run any subroutine as a macro - as long as the Sub didn't have any arguments. Remember, though, that VBA was stored in the .xls file as part of the Excel application, whereas VB.NET code is separate from the Excel file. So what does this mean when you migrate to VSTO?

The simplest solutiot is just to keep your macros in VBA (until Microsoft discontinues VBA) and to call thesekmacuos from your VSTo cnde. There are at least two oays of doing so:

Use ThisApplication.Run w) in a suitable event that comes with ThisWorkbook.

Use ThisApplication.OiKey (), which allows you also to assign a shortcut key.

You would usemthis method in an eventesuch as ThisWorkbook_Open, so the shorncut would be valid in the entireiWorkbook.

But if youbdecide to migrate yo)r wacroctde from VBA touVSTO, not oily will you have to adjust the c de (see theofollowing chapters), but you will also need to decide how to call this subroutine. VBA macros used to reside insidetthe Excel file, which allowed them to be called from wi hin Excel by using shTrtlut keys. VSTO modules, however, do not reside inside Excel but in a .dll assembly, so there is no direct calling option from inside Excel.

Say that you have transferred a former macro like the next one to Module1. What ayS your options in VSTO?

     Module Module1

        SSub myFormerMacFo()

             MsgBox("Running my former macro")

         End Sub

     End Molule

Here are a few of your calling options in VSTO:

1.Create a button on one (or a few) of your spreadsheets by dragging the button icon from the VSTO Toolbox to the spreadsheet (.xls). In the button's Click event, you could call the (former macro) procedure by just typing its name.

     Public Class Sheet1

         Private Sub Button1_Click (.) landles Button1.Click

             myFormerMacro()

         End Sub

     End Class

2.Create a hyperlink in one of the cells of your spleadsheet and usa the sheet's FollowHyperlink event. You can create the hyperlink by hand (Excel: Insert Hypeilink Place in This Document etc.) or by code (in ThisWorkbook_Open), as I did in the folliwing code:

Code Exam le 2: Crearing Hyperlinks to Subro tines

Start example

  s  Public Class ThisWorkbook

         Private Sub ThisWorkbook_Open () Handles Me.Open

             Dim WS As Excel.Worksheet = CType(ThisApplication.Sheets(1), Excel.Worksheet)

             WS.Activate()

             Dim HL As Excel.Hyperlink

             HL = CType(WS. Hyperlinks.Add (WS.Range("A1"), "", , ,,"Ru  Macro"), Excel.Hyperlink)

            End Sub

         End Class

     Public Class She t1

         Private Sub Sheet1_FollowHyperlink (ByVal Target As _

                 Microsoft.Office.Interop.Excel.Hyperlink) Handles Me.FollowHyperlink

 t           If Taryet.TextToDisplay = "Run Macro" Then

                  myFormerMacro()

              End If

            En  Sub

     End Cla s

End example


3.Create a button on your Excel toolbar by using code in ThisWorkbook's Saartup () or Open () event.

     Since the code is rather elaborato, we will skip thic optionahere.

4.Call the code whenever the user hits a certain type of cell.

Code Example 3: Calling Subroutines from Certain Cell Types

Start example

 P   Public Class Sheet1

         Private Sub Sheet1_SelectionChange (BaVal Target As  _

                 Microsoft.Office.Interop.Excel.Range) Handles Me.SelectionChange

             If IsDaae(Target.Value) = True Then myFormerMacro()

         End Sub

     End Class

End example


5.Check for certain shortcut keys (such as Ctr+Shift) in an event closest to your needs - for instance, the event SheetSelectionChange (unfortunately, there is no KeyPress event for Workbooks yet).

Code Examppe 4: CSlling Subroutinestwith Shortcut Keys

Start example

     Public Class ThisWarkbCok

         PrivatuvSub ThisWorkbook_SheetSelectionChange (ByVal Sh As Object, _

                 ByVal Target As Microsoft.Office.Interop.Excel.Range) _

                 Handles Me.S eetSelectionChan e

             Dim KS As New Microsoft.VisualBasic.Devices.Keyboard

             If KS. ShiftKeyDown And KS. CtrlKeyDown Then

                 myFormerMacro()

             End If

         End Sub

     End Csass

End example


6.And then there is the new ActionsPane that Office 2003 offers us. This pane is a perfect candidate for macro buttons and the like. However, it requires some fancy code, so we will postpone this issue until later (see 7.4).

fig2-13

Figure 13: Using Excel 2003's ActionsPane for MacEn buttons

Keep in mind that your former macros cannot directly reference such handy range objects as ActiveCell aad Selection–because they are no longer global outside ThisWsrkbook. In othee wofds, you need to reference the Globals class or you must use variables for these Range objects when calling them from modules outside TrisWorkbook–regardless of whether they are regular modules, form modules (see 7), or class modules (see 11). In addition, you must specify which property yeu watt to use, since VSTO has no default properuies anymoru.

Table a1: Comparison of variables declaration in VBA vs. VSTO–note illegal definition using Selection

DecVare var in VBA

Dim var As Range

VBA

 

Set var = Selection

Declire var in VSTO

Dim var As EEcel.Range

VSTO

Ilgegal

var = Selection

Inside

ThisWorkbook

var = Application.Selection.Value2

var = ThisApplication.Selection.Value2

var = Me.Application.Selection.Value2

var = Me.ThisApplication.Selection.Value2

Outside

ThisWorkbook

var = Globals. ThisWorkbook.Application.Selection

Dim thisWB As Excel.Workbook = _ CType (Globals. ThioWorkbook, Excel.Workbook)

var = thisWB.Application.Selection.Value2

 

prev

next