Using Regular Expressions to Recognize Phrases

As mentioned earlier, you shouldn't add a phrase (multiple words) to the Terms property of a smart tag because the tokens they are matched against contain only one word and thus a match will never be found. One way to have your solution recognize multiple words that are separated by spaces is to add the phrase as a regular expression to the smart tag's Expressions property.

In VSTO, there are several objects, such as SmartTags, ListObjects, and ActionsPanes, that are named by concatenating strings. When used generically, these objects are spelled with a space between words (smart tags, list objects, and actions panes). If you want to create smart tags for each of these spellings, you can use the Terms property for the individual words and use the Expressions property for phrases.

In this section you'll use regular expressions to create a Word customization that recognizes terms and phrases. When the expression is recognized, the smart tag will enable you to perform searches on Windows Live Search for additional information about the recognized text. The search results are displayed in a Web browser control on the actions pane.

Creating the Word Project with VSTO

1. Create a Word document project with VSTO.

2. Right-click the project in Solution Explorer, point to Add, and click New Item.

3. In the New Item dialog box, select Actions Pane Control, and leave the default name ActionsPaneControll.

4. Drag a WebBrowser control from the Toolbox to the actions pane control, and change the Dock property to Fill.

Creating Smart Tags Using Regular Expressions

1. In Solution Explorer, right-click ThisDocument and click View Code.

2. At the top of the code file, import System.Text.RegularExpressions, and then add three class-level variables for the Action, URL string, and actions pane control, as shown in Listing 9.11.

Listing 9.11. Adding terms and expressions to a smart tag Imports System.Text.RegularExpressions

Public Class ThisDocument

' Create class-level variables.

WithEvents LiveSearch As Microsoft.Office.Tools.Word.Action Dim url As String

Dim ap As New ActionsPaneControl1

3. In the Startup event handler of ThisDocument, add the code in Listing 9.12. This code creates the smart tag, adds words and phrases to be recognized, adds the action, and changes the position and size of the task pane.

Listing 9.12. Creating a smart tag with terms and regular expressions

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

' Create new smart tag.

Dim mySmartTag As New Microsoft.Office.Tools.Word.SmartTag( _ "", "sample")

' Add individual words to be recognized as terms. mySmartTag.Terms.Add("ActionsPane") mySmartTag.Terms.Add("SmartTag") mySmartTag.Terms.Add("ListObject")

' Add phrases to be recognized as regular expressions. The ' \b indicates a word boundary.

mySmartTag.Expressions.Add(New Regex("\bactions pane\b")) mySmartTag.Expressions.Add(New Regex("\blist object\b")) mySmartTag.Expressions.Add(New Regex("\bsmart tag\b"))

' Create the action and add it to the smart tag. LiveSearch = New Microsoft.Office.Tools.Word.Action( _

"Windows Live Search") mySmartTag.Actions = New _

Microsoft.Office.Tools.Word.Action(){LiveSearch} Me.VstoSmartTags.Add(mySmartTag)

' Change the position of the task pane to dock at the ' top of the document, and set its height and the ' height of the actions pane control. Me.CommandBars("Task Pane").Position = _

Microsoft.Office.Core.MsoBarPosition.msoBarTop Me.CommandBars("Task Pane").Height = 300 ap.Height = 300 End Sub

4. In the Click event handler of the LiveSearch action, create the search string using the recognized text, and pass it to an OpenActionsPane method to display the search results in the actions pane, as shown in Listing 9.13.

Listing 9.13. Passing the URL to the Web browser control on the actions pane

' Set the URL for the WebBrowser and show it on the actions pane. Private Sub LiveSearch_Click(ByVal sender As Object, ByVal e _ As Microsoft.Office.Tools.Word.ActionEventArgs) _ Handles LiveSearch.Click url = "" & e.Text OpenActionsPane(url)

End Sub

' Display the web browser control on the actions pane if it is ' not already visible; then show search results on Live Search. Private Sub OpenActionsPane(ByVal url As String)

If Me.ActionsPane.Controls.Count < 1 Then Me.ActionsPane.Controls.Add(ap) ap.WebBrowser1.Navigate(url)


Application.CommandBars("Task Pane").Visible = True ap.WebBrowser1.Navigate(url) End If

End Sub

Running the Project Code

1. Press F5 to run the code.

2. Type the text The actions pane is cool. in the document.

3. Hold your cursor over the words actions pane, and click the Windows Live Search button.

When you click Windows Live Search, the actions pane will open at the top of the document and display the search results for the words actions pane. If you click the Video tab in Windows Live Search, you should see a couple of the video demonstrations that we created regarding using the actions pane. Your document should resemble the one in Figure 9.11.

❖ Note If the Video tab is not visible on the Windows Live Search page, click the drop-down titled More, and then click Video.

Figure 9.11. Searching for recognized text on Windows Live Search through use of a smart tag
0 0

Post a comment