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.
- Create a VB or CS Word/Excel project. (I will use Word for this example)
- Add a reference to Microsoft Smart Tags 2.0 Type Library from the COM tab of the Add Reference dialog box.
- Add a new Item to the project. Choose ActionsPane Control. Use the default name of ActionsPaneControl1.
- 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.
- 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");
Comments
- Anonymous
March 30, 2005
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? - Anonymous
March 30, 2005
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. - Anonymous
March 30, 2005
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! - Anonymous
March 30, 2005
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 :-) - Anonymous
March 31, 2005
I believe that you could also use the March CTP build. - Anonymous
July 21, 2005
One quick tip today. Since VSTO Beta2 we have added SmartTags support for document level customizations.... - Anonymous
September 17, 2005
One quick tip today. Since VSTO Beta2 we have added SmartTags support for document level customizations.... - Anonymous
May 01, 2006
Is this still valid code. I did everything as you said and it does not work. - Anonymous
May 01, 2006
Yes. I just retested this code and both the C# and VB smart tags are working. - Anonymous
June 19, 2006
Your article is prety nice. It's a pity that i didn't see it more later. - Anonymous
July 05, 2006
Your article is quite right, thanks. - Anonymous
September 28, 2006
I thought I'd make a little table: VSTO Version Office 2003 Office 2007 Additional Information doc app