Creating the Word Project

1. Create a Word solution using VSTO, and name it OffLineSmartTag.

2. Add a reference to the Microsoft Smart Tags 2.0 Type Library (on the COM tab of the Add Reference dialog box).

3. Add an actions pane control to the project, leaving the default name.

4. Add a WebBrowser control to the actions pane control, leaving the default name.

5. Right-click the project name in Solution Explorer, point to Add, and click Class. Name the class OffLine.

6. Replace the code in the OffLine class with the code in Listing 9.19.

Listing 9.19. Creating a custom smart tag class

Imports Microsoft.Office.Tools.Word Imports Microsoft.Office.Interop.SmartTag

Public Class OffLine Inherits SmartTag

' Create variables for the actions, the URL string, ' and the actions pane control.

WithEvents LiveSearch As New Action("Windows Live Search") Dim url As String

Dim ap As New ActionsPaneControl1

Dim Word As String

Dim Words As New Collection

' In constructor, create the new smart tag and actions. MyBase.New("www.aw.com/Offline#Sample", _

"Offline Example") Actions = New Action() {LiveSearch}

' Add words to the Words collection With Words

.Add("ActionsPane") .Add("ListObject") .Add("SmartTag") End With

' 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. Globals.ThisDocument.CommandBars("Task Pane"). _ Position = Microsoft.Office.Core. _ MsoBarPosition.msoBarTop Globals.ThisDocument.CommandBars("Task Pane"). _

Height = 300 ap.Height = 300

' Override the Recognize method to commit a smart tag. Protected Overrides Sub Recognize(ByVal text As String, _ ByVal site As Microsoft.Office.Interop.SmartTag. _ ISmartTagRecognizerSite, ByVal tokenList As _ Microsoft.Office.Interop.SmartTag.ISmartTagTokenList)

' If the computer is online, add smart tags. If My.Computer.Network.IsAvailable Then For Each Word In Words

' Loop through each term in the tokenList for ' a match and add a smart tag if the Network ' is available.

For i As Integer = 1 To tokenList.Count Dim token As ISmartTagToken = _

tokenList.Item(i) If Word = token.Text Then site.CommitSmartTag(Me.SmartTagType, _ token.Start, token.Length, Nothing)

End If

Next

Next End If

End Sub

' Change the caption to reflect the computer's network ' status.

Private Sub LiveSearch_BeforeCaptionShow( _ ByVal sender As Object, ByVal e As _ Microsoft.Office.Tools.Word.ActionEventArgs) _ Handles LiveSearch.BeforeCaptionShow

If My.Computer.Network.IsAvailable Then

LiveSearch.Caption = "Windows Live Search"

Else

LiveSearch.Caption = _

"Windows Live Search is unavailable (Offline)"

End If End Sub

Private Sub LiveSearch_Click(ByVal sender As Object, _ ByVal e As Microsoft.Office.Tools.Word _ .ActionEventArgs) Handles LiveSearch.Click

' If the computer is online, set the search criteria ' and pass URL to the OpenActionsPane method. If My.Computer.Network.IsAvailable Then url = "http://search.live.com/results.aspx?q=" & _

e.Text OpenActionsPane(url) End If

End Sub

' Display the Web browser control on the actions pane if ' it is not already visible; then show search results on ' Windows Live Search.

Private Sub OpenActionsPane(ByVal url As String)

If Globals.ThisDocument.ActionsPane.Controls.Count _ < 1 Then

Globals.ThisDocument.ActionsPane.Controls.Add(ap) ap.WebBrowser1.Navigate(url)

Else

Globals.ThisDocument.Application.CommandBars( _

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

End Sub

End Class

7. Add the code in Listing 9.20 to the Startup event handler of ThisDocument. This code adds to the VstoSmartTags collection the smart tag created in the custom smart tag class.

Listing 9.20. Adding the smart tag to VstoSmartTags collection

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

Me.VstoSmartTags.Add(New OffLine)

End Sub

0 0

Post a comment