Share via


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

本逐步解說示範如何在 Word 文件層級自訂中建立智慧標籤。 智慧標籤會辨識華氏溫度字串。 智慧標籤所加入的動作會將溫度值轉換成攝氏,然後用格式化的攝氏溫度字串取代所辨識文字。

**適用於:**本主題中的資訊適用於 Word 2007 的文件層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

若要執行這個智慧標籤,使用者必須啟用 Word 中的智慧標籤。 如需詳細資訊,請參閱 HOW TO:在 Word 和 Excel 中啟用智慧標籤

這個逐步解說將說明下列工作:

  • 建立可辨識規則運算式 (Regular Expression) 的智慧標籤。

  • 建立動作,以從智慧標籤擷取資料並修改所辨識的智慧標籤文字。

注意事項注意事項

在下列指示的某些 Visual Studio 使用者介面項目中,您的電腦可能會顯示不同的名稱或位置: 您所擁有的 Visual Studio 版本和使用的設定決定了這些項目。 如需詳細資訊,請參閱 使用設定

必要條件

您需要下列元件才能完成此逐步解說:

-

包含 Microsoft Office 開發者工具的 Visual Studio 2010 版本。 如需詳細資訊,請參閱[設定電腦以開發 Office 方案](bb398242\(v=vs.100\).md)。
  • Word 2007。

  • .NET Framework 3.5:

注意事項注意事項

如果您的目標是 .NET Framework 4,則必須撰寫不同的程式碼以建立智慧標籤和動作。 如需詳細資訊,請參閱 智慧標籤架構

建立新專案

第一步就是建立 Word 文件專案。

若要建立新的專案

Visual Studio 會在設計工具中開啟新的 Word 文件,並將 My Smart Tag 專案加入至 [方案總管]。

設定專案

專案需要有智慧標籤 DLL 的參考,而且還需要使用規則運算式。

若要設定專案

  1. 在 [專案] 功能表上,按一下 [加入參考]。

  2. 在 [.NET] 索引標籤上,選取 [Microsoft.Office.Interop.SmartTag],然後按一下 [確定]。 選取組件的 12.0.0.0 版本。

  3. 在 [方案總管] 中,以滑鼠右鍵按一下 [ThisDocument.vb] (使用 Visual Basic 時) 或 [ThisDocument.cs] (使用 C# 時),然後按一下 [檢視程式碼]。

  4. 在檔案頂端加入下面這行程式碼。

    Imports System.Text.RegularExpressions
    
    using System.Text.RegularExpressions;
    

建立智慧標籤

若要讓智慧標籤能夠尋找和轉換華氏溫度字串,請將規則運算式加入至智慧標籤能夠辨識的項目清單,以及建立當使用者按一下智慧標籤時可使用的動作。

若要建立智慧標籤

  1. 以下列程式碼取代 ThisDocument 類別中的 ThisDocument_Startup 事件處理常式。 此程式碼會建立代表智慧標籤的 SmartTag,並將規則運算式加入至智慧標籤可辨識的項目清單中。

    WithEvents action1 As Microsoft.Office.Tools.Word.Action
    
    Private Sub ThisDocument_Startup(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Startup
    
        'Use the following line of code in projects that target the .NET Framework 4.
        Dim smartTag1 As Microsoft.Office.Tools.Word.SmartTag = _
            Globals.Factory.CreateSmartTag( _
           "www.microsoft.com/Demo#DemoSmartTag", _
           "Demonstration Smart Tag")
    
        'In projects that target the .NET Framework 3.5, use the following line of code.
        'Dim smartTag1 As New Microsoft.Office.Tools.Word.SmartTag( _
        '   "www.microsoft.com/Demo#DemoSmartTag", _
        '  "Demonstration Smart Tag")
    
        smartTag1.Expressions.Add( _
            New Regex("(?'number'[+-]?\b[0-9]+)?\s?(F|f)\b"))
    
    private Microsoft.Office.Tools.Word.Action action1;
    
    private void ThisDocument_Startup(object sender, System.EventArgs e)
    {
        // Use the following line of code in projects that target the .NET Framework 4.
        Microsoft.Office.Tools.Word.SmartTag smartTag1 =
            Globals.Factory.CreateSmartTag(
            "www.microsoft.com/Demo#DemoSmartTag",
            "Demonstration Smart Tag");
    
        // In projects that target the .NET Framework 3.5, use the following line of code.
        //Microsoft.Office.Tools.Word.SmartTag smartTag1 =
        //  new Microsoft.Office.Tools.Word.SmartTag(
        //  "www.microsoft.com/Demo#DemoSmartTag",
        //  "Demonstration Smart Tag");
    
        smartTag1.Expressions.Add(new Regex(
            @"(?'number'[+-]?\b[0-9]+)�?\s?(F|f)\b"));
    
  2. 建立新的 Action,並將它加入至智慧標籤的 Actions 屬性中。 Action 代表使用者可在智慧標籤功能表中按一下的項目。

    'Use the following line of code in projects that target the .NET Framework 4.
    action1 = Globals.Factory.CreateAction( _
        "Convert to Celsius")
    
    'In projects that target the .NET Framework 3.5, use the following line of code.
    'action1 = New Microsoft.Office.Tools.Word.Action( _
    '    "Convert to Celsius")
    
    smartTag1.Actions = _
        New Microsoft.Office.Tools.Word.Action() {action1}
    
    
    // Use the following line of code in projects that target the .NET Framework 4.
    action1 = Globals.Factory.CreateAction(
        "Convert to Celsius");
    
    // In projects that target the .NET Framework 3.5, use the following line of code.
    //action1 = new Microsoft.Office.Tools.Word.Action(
    //    "Convert to Celsius");
    
    
    
    smartTag1.Actions = new
        Microsoft.Office.Tools.Word.Action[] {action1};
    
  3. SmartTag 加入至 VstoSmartTags 屬性中,藉此將智慧標籤附加至文件。 在 C# 中,將事件處理常式附加至動作的 Click 事件中。

        Me.VstoSmartTags.Add(smartTag1)
    End Sub
    
        this.VstoSmartTags.Add(smartTag1);
    
        action1.Click += new
            Microsoft.Office.Tools.Word.ActionClickEventHandler(
            action1_Click);
    }
    

建立動作的事件處理常式

事件處理常式會從 number 索引鍵擷取華氏溫度值,其位於智慧標籤的屬性包中。 然後事件處理常式再將華氏溫度值轉換成攝氏,並取代所辨識的字串。

在這個範例中,number 索引鍵會從指定給智慧標籤的規則運算式中識別擷取的群組。 如需智慧標籤中屬性包和規則運算式的詳細資訊,請參閱智慧標籤架構

若要建立事件處理常式

  • 將下列程式碼複製到 ThisDocument 類別。

    Private Sub action1_Click(ByVal sender As Object, _
        ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
        Handles action1.Click
    
        Dim value As String = e.Properties.Read("number")
        Dim fahrenheit As Double = System.Convert.ToDouble(value)
        Dim celsius As Integer = Fix(fahrenheit - 32) * 5 / 9
        e.Range.Text = celsius.ToString() + "C"
    End Sub
    
    void action1_Click(object sender,
        Microsoft.Office.Tools.Word.ActionEventArgs e)
    {
        string value = e.Properties.get_Read("number");
        double fahrenheit = System.Convert.ToDouble(value);
        int celsius = (int)(fahrenheit - 32) * 5 / 9;
        e.Range.Text = celsius.ToString() + "�C";
    }
    

測試應用程式

現在您可以測試文件,確定智慧標籤會將華氏溫度轉換成攝氏。

若要測試您的活頁簿

  1. 啟用 Word 中的智慧標籤。

    如需詳細資訊,請參閱 HOW TO:在 Word 和 Excel 中啟用智慧標籤

  2. 請按 F5 執行您的專案。

  3. 輸入與加入至智慧標籤的規則運算式一致的字串,例如 60F、60° F 或 60 F。

    注意事項注意事項

    若要輸入溫度符號 (°),請按 ALT 並輸入 248。

  4. 按一下辨識字串上方出現的智慧標籤圖示,再按一下 [換算為攝氏]。

  5. 確定原始字串已由包含攝氏溫度的新字串所取代。

請參閱

工作

HOW TO:在 Word 和 Excel 中啟用智慧標籤

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

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

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

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

逐步解說:使用應用程式層級增益集建立智慧標籤

概念

智慧標籤架構

其他資源

智慧標籤概觀