Share via


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

本逐步解說將示範如何建立應用程式層級的智慧標籤,您可以在每個開啟的文件中使用此智慧標籤。 智慧標籤會辨識 Microsoft Office 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 會將 [我的收據智慧標籤] 專案加入至 [方案總管]。

設定專案

專案需要有智慧標籤 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. 以下列程式碼取代 ThisAddIn 類別中的 ThisAddIn_Startup 事件處理常式。 此程式碼會建立代表智慧標籤的 SmartTag,並將規則運算式加入至智慧標籤可辨識的項目清單中。

    WithEvents RecipeAction As Microsoft.Office.Tools.Word.Action
    
    Private Sub ThisAddIn_Startup(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Startup
    
        Dim SmartTagDemo As New Microsoft.Office.Tools.Word.SmartTag( _
            "www.microsoft.com/Demo#DemoSmartTag", "Recipe Smart Tag")
    
        SmartTagDemo.Expressions.Add(New Regex( _
            "(?'tbsNumber'[+-]?\b[0-9]+)?\s?(tbs|tablespoons|tablespoon)\b"))
    
    private Microsoft.Office.Tools.Word.Action RecipeAction;
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Microsoft.Office.Tools.Word.SmartTag SmartTagDemo =
            new Microsoft.Office.Tools.Word.SmartTag(
            @"www.microsoft.com/Demo#DemoSmartTag",
            @"Recipe Smart Tag");
    
        // Specify the terms to recognize.
        SmartTagDemo.Expressions.Add(new Regex(
            @"(?'tbsNumber'[+-]?\b[0-9]+)?\s?(tbs|tablespoons|tablespoon)\b"));
    
  2. 建立新的 Action,並將它加入至智慧標籤的 Actions 屬性中。 Action 代表使用者可在智慧標籤功能表中按一下的項目。

    RecipeAction = New Microsoft.Office.Tools.Word.Action("Convert to ounces")
    SmartTagDemo.Actions = New Microsoft.Office.Tools.Word.Action() {RecipeAction}
    
    RecipeAction = new Microsoft.Office.Tools.Word.Action(
        @"Convert to ounces");
    
    // Add the action to the smart tag.
    SmartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] { RecipeAction };
    
  3. 將智慧標籤附加至 ThisAddIn 類別的 VstoSmartTags 屬性中。 在 C# 中,將事件處理常式附加至動作的 Click 事件中。

        Me.VstoSmartTags.Add(SmartTagDemo)
    End Sub
    
        // Add the smart tag to the document.
        this.VstoSmartTags.Add(SmartTagDemo);
    
        RecipeAction.Click += new Microsoft.Office.Tools.Word.ActionClickEventHandler(
            RecipeAction_Click);
    }
    

建立動作的事件處理常式

事件處理常式會從智慧標籤之屬性包中的 tbsNumber 索引鍵擷取湯匙值。 事件處理常式接著會將湯匙數量轉換為盎司,並將盎司值插入湯匙值後面的括號中。

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

若要建立事件處理常式

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

    Private Sub RecipeAction_Click(ByVal sender As Object, _
        ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
        Handles RecipeAction.Click
    
        Dim value As String = e.Properties.Read("tbsNumber")
        Dim tbsRecipeAmount As Double = System.Convert.ToDouble(value)
        Dim ozRecipeAmount As Double = tbsRecipeAmount * 0.5
        e.Range.InsertAfter(" (" + ozRecipeAmount.ToString() + " oz)")
    End Sub
    
    private void RecipeAction_Click(object sender,
        Microsoft.Office.Tools.Word.ActionEventArgs e)
    {
        string value = e.Properties.get_Read(@"tbsNumber");
        double tbsRecipeAmount = System.Convert.ToDouble(value);
        double ozRecipeAmount = tbsRecipeAmount * 0.5;
        e.Range.InsertAfter(" (" + ozRecipeAmount.ToString() + " oz)");
    }
    

測試應用程式

現在您可以測試文件,確認智慧標籤會將湯匙度量單位轉換成盎司。

若要測試您的活頁簿

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

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

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

  3. 在 Word 文件中輸入 1 湯匙鹽。

  4. 按一下 1 湯匙上方出現的智慧標籤圖示,再按一下 [轉換成盎司]。

  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 中使用自訂辨識器建立智慧標籤

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

概念

智慧標籤架構

其他資源

智慧標籤概觀