Creating Word and Excel Smart Tags with VSTO

Because VSTO smart tags are available only to the document or workbook in which you add them, you can scope the smart tags to a specific solution rather than create smart tags that might be available to any document that is opened.

VSTO provides two smart tag classes (Microsoft.Office.Tools.Excel.SmartTag and Microsoft.Office.Tools.Word.SmartTag) that enable you to add smart tags to your document or workbook. You enable Word or Excel to recognize certain terms by using these classes to add strings to the Terms property or to add regular expressions to the Expressions property of the smart tag. You'll learn more about regular expressions later in this chapter in "Introduction to Regular Expressions."

When you create a new smart tag, you create an instance of the SmartTag class and pass a unique identifier and a name (caption) for the smart tag. The identifier uses the following constructions: URI namespace#Tag name. For example, you might pass the following:

"www.aw.com/Sample#SampleSmartTag"

Here, the URI namespace is www.aw.com, and the tag name is Sample Smart Tag. To specify actions to be performed on the recognized text, you specify actions in the Actions property of the smart tag instance, and then you add the smart tag instance to the VstoSmartTags collection for the document. Let's look at a simple example.

1. Create an Excel workbook project with VSTO.

2. In Solution Explorer, right-click ThisWorkbook and click View Code.

3. Add the code in Listing 9.3 to the code file after the line Public Class Workbook. This code adds a variable that has events for a Microsoft.Office.Tools.Excel.Action.

Listing 9.3. Adding a variable for a smart tag action

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

4. Add the code in Listing 9.4 to the Startup event handler of the ThisWorkbook class. This code turns on the smart tag feature, creates a variable named ControlSmartTag for an Excel smart tag, and then adds a number of terms to the smart tag's recognizer. The code then adds an action to the smart tag and adds the smart tag to the workbook.

Listing 9.4. 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")

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

' Add the smart tag to the VstoSmartTags collection. Me.VstoSmartTags.Add(ControlSmartTag)

End Sub

The terms that we added to the smart tag are control names. The action we'll create for the smart tag will add the selected control to the actions pane. Add code to the Click event handler of the AddControlToActionsPane action, which will add the control to the actions pane, as shown in Listing 9.5. This code should also be added to the ThisWorkbook class.

Listing 9.5. Adding code to event handlers of the 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

5. Next, right-click Sheet1 in Solution Explorer, and then click View Designer.

6. Add the following words to cells in Sheet1, as shown in Figure 9.5: Button, ComboBox, ListBox, DateTimePicker, MonthCalendar.

Figure 9.5. Adding control names to the worksheet

7. Press F5 to run the code.

When the solution runs, you'll notice that all the cells that contain control names have a smart tag indicator (the little triangle). When you move your cursor above the cell, the Smart Tag Actions button becomes visible. If you select the drop-down arrow on this button, an action called Add Control to Actions Pane appears. When you click the action, the control is added to the actions pane. Figure 9.6 illustrates this with a number of controls added to the actions pane.

|e3 Microsoft Excel -

:HcelWorkbook2.Hls

3'] File Edit View Insert Format Tools Data Window Help

Type a question for help ».fix

J A "I Arial

-io b I u 1 m m m a | $ % »

:== if if • a» • a • 1

D14

f*

A

B

c

D

E

F

G

Document Actions T X

g © □

1

2

Button ,

3

4

ComboBoï

5

6

ListBox j

Sun Mon Tue Wed Thu Fri Sat 30 31 1 2 3 4 5 6 7 8 S 10 11 12 13 14 15 16 17 18 18 Bil 21 22 23 24 25 26 27 28 28 30 31 1 2 3 4 5 6 7 8 8 □ Today: 8/20/2006

7

8

DateTirne^

«1

3

Control Smart Tag: DateTimePicker

in

MonthCalg

11

Add Control to Actions Pane .

12

Remove this Smart Tag

Stop Recognizing "DateTimePicker" ►

13

14

15

Smart Tag Options...

lb

17

18

13

20

21

22

23

24

25

26

17

H 1 ► ni\Sheetl / 5heet2 / 5heet3 / M I Ml

1-

Ready

A

Figure 9.6. Adding controls to the actions pane via a smart tag

Figure 9.6. Adding controls to the actions pane via a smart tag

0 0

Responses

  • semolina
    What are smart tags in word?
    2 years ago

Post a comment