SmartTag 类 (2007 system)

更新:2007 年 11 月

表示 Microsoft Office Word 的文档级自定义项中的智能标记。

命名空间:  Microsoft.Office.Tools.Word
程序集:  Microsoft.Office.Tools.Word.v9.0(在 Microsoft.Office.Tools.Word.v9.0.dll 中)

语法

声明
<PermissionSetAttribute(SecurityAction.Demand, Name := "FullTrust")> _
Public Class SmartTag _
    Inherits SmartTagBase
用法
Dim instance As SmartTag
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public class SmartTag : SmartTagBase

备注

SmartTag 类是代表 Word 文档中的智能标记的抽象 SmartTagBase 类的实现。有关 Visual Studio Tools for Office 解决方案中的智能标记的信息,请参见智能标记的结构

示例

下面的代码示例演示如何创建您自己的从 SmartTag 派生的自定义智能标记类。该示例重写 Recognize 方法以识别在某个段落中找到的每个智能标记术语。此示例假设您已从“添加引用”对话框的“COM”选项卡添加一个对“Microsoft Smart Tags 2.0 类型库”的引用。有关使用 SmartTag 的默认实现的代码示例,请参见 Action 类。

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

Public Class CustomSmartTag
    Inherits SmartTag

    ' Declare Actions for this SmartTag
    WithEvents Action1 As New Action("Display property value")
    WithEvents Action2 As New Action("Display smart tag details")

    Public Sub New()
        MyBase.New("https://www.contoso.com/Demo#DemoSmartTag", _
            "Custom Smart Tag")
        Me.Terms.AddRange(New String() {"sales", "organization"})
        Actions = New Action() {Action1, Action2}
    End Sub

    Protected Overrides Sub Recognize(ByVal text As String, _
        ByVal site As ISmartTagRecognizerSite, _
        ByVal tokenList As ISmartTagTokenList)

        ' Determine whether each smart tag term exists in 
        ' the document text.
        Dim Term As String
        For Each Term In Me.Terms

            ' Search the text for the current smart tag term.
            Dim index As Integer = Me.ParagraphText.IndexOf(Term, 0)

            While (index >= 0)

                ' Create a smart tag token and a property bag for the 
                ' recognized term.
                Dim propertyBag As ISmartTagProperties = _
                    site.GetNewPropertyBag()

                ' Write a new property value.
                Dim key As String = "Key1"
                propertyBag.Write(key, DateTime.Now)

                ' Attach the smart tag to the term in the document
                Me.PersistTag(index, Term.Length, propertyBag)

                ' Increment the index and then find the next
                ' instance of the smart tag term.
                index += Term.Length
                index = ParagraphText.IndexOf(Term, index)
            End While
        Next
    End Sub

    ' This action displays the property value for the term.
    Private Sub Action1_Click(ByVal sender As Object, _
        ByVal e As ActionEventArgs) Handles Action1.Click

        Dim propertyBag As ISmartTagProperties = e.Properties
        Dim key As String = "Key1"
        MsgBox("The corresponding value of " & _
            key & " is: " & propertyBag.Read(key))
    End Sub

    ' This action displays smart tag details.
    Private Sub Action2_Click(ByVal sender As Object, _
        ByVal e As ActionEventArgs) Handles Action2.Click

        MsgBox("The current smart tag caption is '" & _
            Me.Caption & "'. The current smart tag type is '" & _
            Me.SmartTagType & "'.")
    End Sub
End Class
using System;
using System.Windows.Forms;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Interop.SmartTag;

public class CustomSmartTag : Microsoft.Office.Tools.Word.SmartTag
{

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

    public CustomSmartTag() : base(
        "https://www.contoso.com/Demo#DemoSmartTag", 
        "Custom Smart Tag")
    {
        this.Terms.AddRange(new string[] { 
            "sales", "organization" });
        Actions = new Microsoft.Office.Tools.Word.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)
    {
        foreach (string term in this.Terms)
        {
            // Search the text for the current smart tag term.
            int index = this.ParagraphText.IndexOf(term, 0);

            while (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(index, term.Length, propertyBag);

                // Increment the index and then find the next
                // instance of the smart tag term.
                index += term.Length;
                index = ParagraphText.IndexOf(term, index);
            }
        }
    }

    // This action displays the property value for the term.
    private void Action1_Click(object sender, 
        Microsoft.Office.Tools.Word.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, 
        Microsoft.Office.Tools.Word.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.Word.SmartTag

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

另请参见

参考

SmartTag 成员

Microsoft.Office.Tools.Word 命名空间

其他资源

智能标记的结构

如何:向 Word 文档添加智能标记

如何:在 Word 中创建具有自定义识别器的智能标记

演练:使用文档级自定义项创建智能标记