Taking Action on a Smart

The reason you add a smart tag to a particular term or phrase is to allow users to take further action based on the recognized term. For example, with Word's built-in smart tags, you can send an e-mail addressed to the recognized name, add the name to your contact list, schedule a meeting with the person, and so on.

You can also create multiple actions for the smart tags that you create. You do this by passing an array of actions to the smart tag's Actions property. Because you must pass an array of actions, the maximum number of actions a smart tag can display is predetermined, and you cannot add actions dynamically.

Using the code example in Listing 9.4, let's add another action. First, we add another variable with events for an action. The new code is displayed in bold font in Listing 9.6.

Listing 9.6. Adding a variable for a smart tag action

WithEvents AddControlToActionsPane As _ Microsoft.Office.Tools.Excel.Action

WithEvents AddControlToDocument As _

Microsoft.Office.Tools.Excel.Action

Next, add the code in bold font in Listing 9.7 to the Startup event handler of ThisWorkbook. This code creates a new action and passes it to the Actions property of the smart tag as another element in the array of actions.

Listing 9.7. Creating a smart tag in Excel

Private Sub ThisWorkbook_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup

' Ensure that smart tags in workbook will be recognized and ' displayed.

Me.Application.AutoCorrect.DisplayAutoCorrectOptions = True Me.SmartTagOptions.DisplaySmartTags = _

Excel.XlSmartTagDisplayMode.xlIndicatorAndButton

' Create an instance of a smart tag, passing unique ' identifier and caption for the smart tag. Dim ControlSmartTag As New Microsoft.Office.Tools.Excel. _ SmartTag("www.aw.com/Sample#SampleSmartTag", _ "Control Smart Tag")

' Add a number of terms (strings) to be recognized. With ControlSmartTag

.Terms.Add("Button") .Terms.Add("ComboBox")

.Terms.Add("ListBox")

.Terms.Add("DateTimePicker")

.Terms.Add("MonthCalendar")

End With

' Add an action for the recognized term.

AddControlToActionsPane = New Microsoft.Office.Tools _ .Excel.Action("Add Control to Actions Pane")

AddControlToDocument = New Microsoft.Office.Tools.Excel _ .Action("Add Control to Document")

ControlSmartTag.Actions = New Microsoft.Office.Tools.Excel _ .Action() {AddControlToActionsPane, AddControlToDocument}

' Add the smart tag to the VstoSmartTags collection.

Me.VstoSmartTags.Add(ControlSmartTag)

End Sub

Then add code to the click event handler of the AddControlToDocument action, as shown in Listing 9.8.

Listing 9.8. Adding code to event handlers of an action

Private Sub AddControlToActionsPane_Click(ByVal sender As _

Object, ByVal e As Microsoft.Office.Tools.Excel. _

ActionEventArgs) Handles AddControlToActionsPane.Click

Select Case e.Text Case "Button"

Me.ActionsPane.Controls.Add(New Button) Case "ComboBox"

Me.ActionsPane.Controls.Add(New ComboBox) Case "ListBox"

Me.ActionsPane.Controls.Add(New ListBox) Case "DateTimePicker"

Me.ActionsPane.Controls.Add(New DateTimePicker)

Case ("MonthCalendar")

Me.ActionsPane.Controls.Add(New MonthCalendar) End Select

End Sub

Private Sub AddControlToDocument_Click(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Excel.ActionEventArgs) _ Handles AddControlToDocument.Click

Select Case e.Text Case "Button"

Globals.Sheet1.Controls.AddButton(e.Range, _

"Control" & (Globals.Sheet1.Controls.Count + _ 1).ToString) Case "ComboBox"

Globals.Sheet1.Controls.AddComboBox(e.Range, _

"Control" & (Globals.Sheet1.Controls.Count + _ 1).ToString) Case "ListBox"

Globals.Sheet1.Controls.AddListBox(e.Range, _

"Control" & (Globals.Sheet1.Controls.Count + _ 1).ToString) Case "DateTimePicker"

Globals.Sheet1.Controls.AddDateTimePicker(e.Range, "Control" & (Globals.Sheet1.Controls.Count + _ 1).ToString) Case ("MonthCalendar")

Globals.Sheet1.Controls.AddMonthCalendar(e.Range, "Control" & (Globals.Sheet1.Controls.Count + _ 1).ToString)

End Select e.Range.Value = "

Now when you run this code and the control names are recognized, you are given two actions in the Smart Tag Actions button, as shown in Figure 9.8: adding the control to the action pane, and adding the control to the document in place of the smart tag.

^ Microsoft Excel - EHcelSmartTag.Hls

File Edit View Insert Format Tools Data Window Help

â 3 i£) V, Favorites' Go- fl C:\Documentsand5ettir j J j ij ji ^ai* ^ a- j\ -0 -it,

File Edit View Insert Format Tools Data Window Help

â 3 i£) V, Favorites' Go- fl C:\Documentsand5ettir j J j ij ji ^ai* ^ a- j\ -0 -it,

A

B

C

D

E

F

1

2

Button ,

3

4

ComboBoj

«1

5

Control Smart Tag: ComboBox

6

ListBox j

7

Add Control to Actions Pane

8

DateTirne^

Add Control to Dott^nent

y

Remove this Smart Tag Stop Recognizing "ComboBox" ►

10

MonthCalg

11

12

13

-

14

15

16

17

1R

Figure 9.8. Displaying multiple actions in a smart tag

Figure 9.8. Displaying multiple actions in a smart tag

Figure 9.9 shows the document with a combo box added to the actions pane, and a month calendar control added to the document.

Suppose you want to change the caption in the Smart Tag Actions button drop-down menu to a cascading menu. To do this, you append a main caption followed by three forward slashes (///) to the action caption. For example, in our smart tag code sample, we have two captions: "Add Control to Actions Pane" and "Add Control to Document." You can change this so that the main caption reads "Add Control" and then the cascading menu contains two items: "to Actions Pane" and "to Document." Change the code in the Startup event handler of ThisWorkbook to match the bold code in Listing 9.9.

E3 Microsoft Excel - EHcelSmartTaq.xls

-inlxi

§i°] File Edit View Insert Format Tools Data Window Help

Type a question for help t _ 5 x

i © ® 1 1 fl Favorites" | Go" ^ | C:\Documentsand5ettings\kmcgrath\MyDocuments\Visual5tudio;" _

lES 4J 1 ^

I

¡Arial

■■ fi B I uj^BE m a J %

, i% Ï" iw A • B

A

B

c

D

E

F

G

H

Document Actions T X

1

2

Button

3

d

4

ComboBoj

5

6

LIstBoii

7

S

DateTimePicker

3

1

10 ITH

11 b^HI

lay. 2006

n

12

Sun Mon Tue Wed Thu Fri Sat 30 1 2 3 4 5 S 7 8 3 10 11 12 13 14 15 16 17 1a 19 20 21 22 23 24 29% 26 27 26 29 30 El 1 2 3 4 5 6 7 B 9 10 □ Today: 5/31/2006

13

14

15

16

17

18

13

20

21

22

23

24

25

rj

26 H 4

► M Ml''

etl /"sheet;

I / Sheet3 j

I'

J

J

Ready

Figure 9.9. Using a smart tag to add controls to the document and to the actions pane

Listing 9.9. Creating a smart tag in Excel

Private Sub ThisWorkbook_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup

' Ensure that smart tags in workbook will be recognized and ' displayed.

Me.Application.AutoCorrect.DisplayAutoCorrectOptions = True Me.SmartTagOptions.DisplaySmartTags = _

Excel.XlSmartTagDisplayMode.xlIndicatorAndButton

' Create an instance of a smart tag, passing unique ' identifier and caption for the smart tag. Dim ControlSmartTag As New Microsoft.Office.Tools.Excel. _ SmartTag("www.aw.com/Sample#SampleSmartTag", _ "Control Smart Tag")

' Add a number of terms (strings) to be recognized.

With ControlSmartTag

.Terms.Add("Button")

.Terms.Add("ComboBox")

.Terms.Add("ListBox")

.Terms.Add("DateTimePicker")

.Terms.Add("MonthCalendar")

End With

' Add an action for the recognized term.

AddControlToActionsPane = New Microsoft.Office.Tools.Excel. _ Action("Add Control///to Actions Pane")

AddControlToDocument = New Microsoft.Office.Tools.Excel _ .Action("Add Control///to Document")

ControlSmartTag.Actions = New Microsoft.Office.Tools.Excel _ .Action() {AddControlToActionsPane, AddControlToDocument}

' Add the smart tag to the VstoSmartTags collection.

Me.VstoSmartTags.Add(ControlSmartTag)

End Sub

When you run the code, the Smart Tag Actions button will contain the options shown in Figure 9.10.

0 0

Post a comment