SmartTag 介面

表示使用 Visual Studio 的 Office 開發工具所自訂之 Excel 活頁簿中的智慧標籤。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)

語法

'宣告
<GuidAttribute("f210dc7f-21b5-475e-ae31-76a9b06b9835")> _
Public Interface SmartTag _
    Inherits SmartTagBase
[GuidAttribute("f210dc7f-21b5-475e-ae31-76a9b06b9835")]
public interface SmartTag : SmartTagBase

SmartTag 型別會公開下列成員。

屬性

  名稱 說明
公用屬性 Actions 取得或設定智慧標籤所公開 (Expose) 的動作陣列。 (繼承自 SmartTagBase)。
公用屬性 Caption 取得智慧標籤的名稱。 (繼承自 SmartTagBase)。
公用屬性 DefaultExtension 取得這個 SmartTag 物件的預設副檔名。
公用屬性 Expressions 取得規則運算式的集合,智慧標籤會辨識這些規則運算式。 (繼承自 SmartTagBase)。
公用屬性 Extension 取得這個 SmartTag 物件的自訂副檔名。
公用屬性 SmartTagType 取得做為智慧標籤唯一識別項的命名空間。 (繼承自 SmartTagBase)。
公用屬性 Terms 取得字串常值 (String Literal) 的集合,智慧標籤會辨識這些字串常值。 (繼承自 SmartTagBase)。

回頁首

方法

  名稱 說明
公用方法 Remove 從智慧標籤中移除規則運算式 (Regular Expression) 辨識器。 (繼承自 SmartTagBase)。

回頁首

備註

若要建立智慧標籤,請使用 Globals.Factory.CreateSmartTag 方法建立 SmartTag 物件。 如需詳細資訊,請參閱智慧標籤架構

注意事項注意事項

此介面是由 Visual Studio Tools for Office Runtime 所實作, 並不能實作於您的程式碼中。 如需詳細資訊,請參閱 Visual Studio Tools for Office Runtime 概觀

使用方式

這個型別僅適用於 Excel 2007 專案。 智慧標籤在 Excel 2010 中已被取代。 如需詳細資訊,請參閱智慧標籤概觀

本文件說明此型別用於以 .NET Framework 4 為目標之 Office 專案的版本。在以 .NET Framework 3.5 為目標的專案中,此型別可能會有不同的成員,而為此型別提供的程式碼範例可能無法運作。 如需此型別在以 .NET Framework 3.5 為目標之專案中的相關文件,請參閱下列 Visual Studio 2008 文件中的參考章節:https://go.microsoft.com/fwlink/?LinkId=160658 (英文)。

範例

下列程式碼範例會建立具備 Action 的 SmartTag,此智慧標籤會辨認 "sale" 詞彙和規則運算式 (Regular Expression) "[I|i]ssue\s\d{5,6}"。 動作會修改在執行階段時動作的功能表標題,並且顯示已辨認文字的位址。 若要測試範例,請在一個儲存格內輸入詞彙 "sale",另一個儲存格內輸入 "issue 12345",然後嘗試執行智慧標籤動作。

WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Excel.SmartTag = _
        Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag")

    ' For .NET Framework 3.5 projects, use the following code to create the smart tag.
    ' Dim smartTagDemo As New  _
    '    Microsoft.Office.Tools.Excel.SmartTag( _
    '    "www.microsoft.com/Demo#DemoSmartTag", _
    '    "Demonstration Smart Tag")

    ' Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale")
    smartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced")

    ' For .NET Framework 3.5 projects, use the following code to create the action.
    ' displayAddress = New Microsoft.Office.Tools.Excel.Action("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Excel.Action() { _
            displayAddress}

    ' Add the smart tag.
    Me.VstoSmartTags.Add(smartTagDemo)
End Sub

Private Sub DisplayAddress_BeforeCaptionShow(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Excel.ActionEventArgs) _
    Handles DisplayAddress.BeforeCaptionShow

    Dim clickedAction As Microsoft.Office.Tools.Excel.Action = _
        TryCast(sender, Microsoft.Office.Tools.Excel.Action)

    If clickedAction IsNot Nothing Then
        clickedAction.Caption = "Display the address of " & e.Text
    End If
End Sub

Private Sub DisplayAddress_Click(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Excel.ActionEventArgs) _
    Handles DisplayAddress.Click

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub
private Microsoft.Office.Tools.Excel.Action displayAddress;

private void AddSmartTag()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Excel.SmartTag smartTagDemo =
        Globals.Factory.CreateSmartTag(
            "www.microsoft.com/Demo#DemoSmartTag",
            "Demonstration Smart Tag");

    // For .NET Framework 3.5 projects, use the following code to create the smart tag.
    // Microsoft.Office.Tools.Excel.SmartTag smartTagDemo =
        // new Microsoft.Office.Tools.Excel.SmartTag(
        //     "www.microsoft.com/Demo#DemoSmartTag",
        //     "Demonstration Smart Tag");

    // Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale");
    smartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

    // Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced");

    // For .NET Framework 3.5 projects, use the following code to create the action.
    // displayAddress = new Microsoft.Office.Tools.Excel.Action("To be replaced");

    // Add the action to the smart tag.
    smartTagDemo.Actions = new Microsoft.Office.Tools.Excel.Action[] { 
        displayAddress };

    // Add the smart tag.
    this.VstoSmartTags.Add(smartTagDemo);

    displayAddress.BeforeCaptionShow += new 
        Microsoft.Office.Tools.Excel.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    displayAddress.Click += new 
        Microsoft.Office.Tools.Excel.ActionClickEventHandler(
        DisplayAddress_Click);
}

void DisplayAddress_BeforeCaptionShow(object sender, 
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    Microsoft.Office.Tools.Excel.Action clickedAction =
        sender as Microsoft.Office.Tools.Excel.Action;

    if (clickedAction != null)
    {
        clickedAction.Caption = "Display the address of " +
            e.Text;
    }
}

void DisplayAddress_Click(object sender, 
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

請參閱

參考

Microsoft.Office.Tools.Excel 命名空間

其他資源

智慧標籤架構

HOW TO:在 Excel 活頁簿中加入智慧標籤

HOW TO:在 Excel 和 .NET Framework 3.5 中使用自訂辨識器建立智慧標籤