HowTo: Create a SmartTag in VB or C# in less than 10 lines of code

Creating SmartTags for Word and Excel using VSTO 2005 has become incredibly simple. I will show you how to create a SmartTag that interacts with an ActionsPane in less than 10 lines of code, including adding the ActionsPane. One of the hardest things to do in Office development has now become one of the easiest. SmartTag development in Office was just plain hard to implement and understand. It was difficult to understand the relationship between recognizers and actions. Well, the good news is that is VSTO 2005 all of that has changed. Now creating a SmartTag is as easy as creating a SmartTag object and setting some properties. So let’s get started.

  1. Create a VB or CS Word/Excel project. (I will use Word for this example)
  2. Add a reference to Microsoft Smart Tags 2.0 Type Library from the COM tab of the Add Reference dialog box.
  3. Add a new Item to the project. Choose ActionsPane Control. Use the default name of ActionsPaneControl1.
  4. Add a Label to the ActionsPane Control. Use the default name of Label1. Set the Label Modifers property to Public (so we can access it from our document.
  5. Add the following code to the code behind for the document

So when you run this code it will recognize the term Hello and have one menu item that sets the label of the actionsPane to the recognized text, in this case “Hello”. I wanted to show you a simple example to get started but you really have full control over the ActionsPane when your Action is fired to do anything you want.

VB Source

Imports Microsoft.Office.Tools.Word

Public Class ThisDocument

    Dim APC As New ActionsPaneControl1

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

        'Create the SmartTag

        Dim ST As New SmartTag("https://MySmartTag/ST#SmartTagToActionsPane", "SmartTag to ActionsPane")

        'define the terms to recognize

        ST.Terms.Add("Hello")

        'create Actions. An Action is a menu item for the SmartTag

        Dim AddtoActionsPaneAction As New Action("Add text to Actions Pane")

        'add the Actions to your SmartTag

        ST.Actions = New Action() {AddtoActionsPaneAction}

        'add the event handler (this could have been done using withevents also)

        AddHandler AddtoActionsPaneAction.Click, AddressOf AddtoActionsPaneAction_Click

        'add the SmartTag to your Document

        Me.VstoSmartTags.Add(ST)

        'add the actionspane

        Me.ActionsPane.Controls.Add(APC)

    End Sub

    Public Sub AddtoActionsPaneAction_Click(ByVal sender As Object, ByVal e As ActionEventArgs)

        APC.Label1.Text = e.Text

    End Sub

End Class

 

Here is the C# source which is nearly identical to the VB source

using System;

using System.Data;

using System.Drawing;

using System.Windows.Forms;

using Microsoft.VisualStudio.Tools.Applications.Runtime;

using Word = Microsoft.Office.Interop.Word;

using Office = Microsoft.Office.Core;

using Microsoft.Office.Tools.Word;

namespace SmartTagToActionPaneCS

{

    public partial class ThisDocument

    {

            ActionsPaneControl1 APC = new ActionsPaneControl1();

        private void ThisDocument_Startup(object sender, System.EventArgs e)

        {

                  //Create the SmartTag

                 

SmartTag ST

= new SmartTag("https://MySmartTag/ST#SmartTagToActionsPane", "SmartTag to ActionsPane");