Welcome to MSDN Blogs Sign in | Join | Help

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("http://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("http://MySmartTag/ST#SmartTagToActionsPane", "SmartTag to ActionsPane");

              //define the terms to recognize

                  ST.Terms.Add("Hello");

                  //create Actions

                  Action AddtoActionsPaneAction = new Action("Add text to Actions Pane");

                  //add the Actions to your SmartTag

                  ST.Actions = new Action[] { AddtoActionsPaneAction };

                  //add the event handler

                  AddtoActionsPaneAction.Click+=new ActionClickEventHandler(AddtoActionsPaneAction_Click);

 

                  //add the SmartTag to your Document

                  this.VstoSmartTags.Add(ST);

 

                  //Add the Actions Pane

                  this.ActionsPane.Controls.Add(APC);

        }

 

            void AddtoActionsPaneAction_Click(object sender, ActionEventArgs e)

            {

                  APC.label1.Text = e.Text;

            }

 

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

        {

        }

 

        #region VSTO Designer generated code

 

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InternalStartup()

        {

            this.Startup += new System.EventHandler(ThisDocument_Startup);

            this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);

        }

       

        #endregion

 

    }

}

 

Published Wednesday, March 30, 2005 10:29 AM by pstubbs
Filed under: ,

Comments

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

May I take the wild guess that this doesn't work in beta 1? I wasn't able to find the ActionsPaneControl anywhere, for step 3. Or where am I to look?
Thursday, March 31, 2005 4:57 AM by Oliver Sturm

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

Right the VST SmartTag feature was added post Beta 1. The ActionsPaneControl is a standard UserControl template. So you can also just create a UserControl. To add this just right clisk on the project and choose Add item, new item.
Thursday, March 31, 2005 6:47 AM by Paul Stubbs

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

Hm... that may work, but I can't use much, if any, of the source code in the sample. There's no SmartTag object (just an interface of the same name) and no Action (just System.Action<T>, which isn't meant here, I assume?). All the namespaces are full of interfaces, but nearly devoid of any classes that could be instantiated. There's also no Terms property on the SmartTag interface.

Well, never mind. I reckon beta 2 will finally be available one of these days, I can try it then. Thanks!
Thursday, March 31, 2005 7:04 AM by Oliver Sturm

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

Rereading your reply, I guess you were actually saying that the whole SmartTag support is just not there in beta 1. That's what I found, too :-)
Thursday, March 31, 2005 7:44 AM by Oliver Sturm

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

I believe that you could also use the March CTP build.
Thursday, March 31, 2005 8:18 AM by Paul Stubbs

# Menus for VSTO Smart Tags are dynamic


One quick tip today. Since VSTO Beta2 we have added SmartTags support for document level customizations....
Thursday, July 21, 2005 1:53 PM by Misha Shneerson

# VSTO 2005 : un brin de fraicheur pour le beta testeur

Friday, July 22, 2005 6:06 AM by The Mit's Blog

# Menus for VSTO Smart Tags are dynamic


One quick tip today. Since VSTO Beta2 we have added SmartTags support for document level customizations....
Sunday, September 18, 2005 2:54 AM by Misha Shneerson

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

Is this still valid code. I did everything as you said and it does not work.
Monday, May 01, 2006 5:38 PM by E.P.

# The code is still good

Yes. I just retested this code and both the C# and VB smart tags are working.
Monday, May 01, 2006 7:27 PM by Paul Stubbs

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

Your article is prety nice. It's a pity that i didn't see it more later.
Monday, June 19, 2006 8:26 PM by rape stories

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

Your article is quite right, thanks.
Thursday, July 06, 2006 1:11 AM by rape videos

# Which VSTO version does what?

I thought I'd make a little table: VSTO Version Office 2003 Office 2007 Additional Information doc app
Thursday, September 28, 2006 3:15 PM by Wouter van Vugt
Anonymous comments are disabled
 
Page view tracker