SmartTag 類別 (2007 系統)

更新: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 Type Library]。如需使用 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

執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

請參閱

參考

SmartTag 成員

Microsoft.Office.Tools.Word 命名空間

其他資源

智慧標籤架構

HOW TO:將置智慧標籤加入至 Word 文件

HOW TO:在 Word 中使用自訂辨識器建立智慧標籤

逐步解說:使用文件層級自訂建立智慧標籤