Export (0) Print
Expand All

SmartTag Class (2007 System)

Represents a smart tag in Microsoft Office Excel.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v9.0 (in Microsoft.Office.Tools.Excel.v9.0.dll)

[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public class SmartTag : SmartTagBase

The SmartTag class is an implementation of the abstract SmartTagBase class for smart tags on Excel workbooks. For information about smart tags in Visual Studio Tools for Office solutions, see Smart Tags Architecture.

The following code example demonstrates how to create your own custom smart tag class that derives from SmartTag. The example overrides the Recognize method to recognize the first smart tag term found in a cell. This example assumes that you have added a reference to Microsoft Smart Tags 2.0 Type Library from the COM tab of the Add Reference dialog box. For a code example that uses the SmartTag class directly, see the Action class.

using System;
using System.Windows.Forms;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Interop.SmartTag;

namespace Trin_ExcelDerivedSmartTags
{
    public class CustomSmartTag : SmartTag {

        // Declare Actions for this SmartTag
        Microsoft.Office.Tools.Excel.Action Action1 =
            new Microsoft.Office.Tools.Excel.Action("Display property value");
        Microsoft.Office.Tools.Excel.Action Action2 =
            new Microsoft.Office.Tools.Excel.Action("Display smart tag details");

        public CustomSmartTag() : base(
            "http://www.contoso.com/Demo#DemoSmartTag", 
            "Custom Smart Tag")
        {
            this.Terms.AddRange(new string[] { 
                "sales", "organization" });
            Actions = new Microsoft.Office.Tools.Excel.Action[] { Action1, Action2 };
            Action1.Click +=
                new ActionClickEventHandler(Action1_Click);
            Action2.Click += 
                new ActionClickEventHandler(Action2_Click);
        }

        protected override void Recognize(string text, 
            ISmartTagRecognizerSite site, ISmartTagTokenList tokenList)
        {
            // Determine whether each smart tag term exists in  
            // the document text. 
            foreach (string term in this.Terms)
            {
                // Search the cell text for the first instance of  
                // the current smart tag term. 
                int index = this.CellText.IndexOf(term, 0);

                if (index >= 0)
                {
                    // Create a smart tag token and a property bag for the  
                    // recognized term.
                    ISmartTagProperties propertyBag = 
                        site.GetNewPropertyBag();

                    // Write a new property value.                  
                    string key = "Key1";
                    propertyBag.Write(key, DateTime.Now.ToString());

                    // Attach the smart tag to the term in the document 
                    this.PersistTag(propertyBag);

                    // This implementation only finds the first instance 
                    // of a smart tag term in the cell.  
                    break;
                }
            }
        }

        // This action displays the property value for the term. 
        private void Action1_Click(object sender, ActionEventArgs e)
        {
            ISmartTagProperties propertyBag = e.Properties;
            string key = "Key1";
            MessageBox.Show("The corresponding value of " + key +
                " is: " + propertyBag.get_Read(key));
        }

        // This action displays smart tag details. 
        private void Action2_Click(object sender, ActionEventArgs e)
        {
            MessageBox.Show("The current smart tag caption is '" + 
                this.Caption + "'. The current smart tag type is '" + 
                this.SmartTagType + "'.");
        }
    }
}

System.Object
  Microsoft.Office.Tools.SmartTagBase
    Microsoft.Office.Tools.Excel.SmartTag

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2015 Microsoft