演练:使用应用程序级外接程序创建智能标记

更新:2007 年 11 月

适用对象

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 应用程序级项目

Microsoft Office 版本

  • Word 2007

有关更多信息,请参见按应用程序和项目类型提供的功能

此演练演示如何创建在每一个打开的文档中均可使用的应用程序级智能标记。此智能标记识别 Microsoft Office Word 2007 文档中的汤匙度量值,并提供将相应的数量换算成盎司的操作。它在以汤匙表示的数量后添加等价的盎司量并用圆括号将盎司量括起来。

若要运行此智能标记,最终用户必须在 Word 中启用智能标记。有关更多信息,请参见如何:在 Word 和 Excel 中启用智能标记

本演练阐释以下任务:

  • 创建使用正则表达式识别字符串的智能标记。

  • 创建可从智能标记检索数据并修改所识别的智能标记文本的操作。

Cc442944.alert_note(zh-cn,VS.90).gif说明:

以下说明中的某些 Visual Studio 用户界面元素在计算机上出现的名称或位置可能会不同。您安装的 Visual Studio 版本以及使用的设置决定了这些元素。有关更多信息,请参见 Visual Studio 设置

先决条件

您需要以下组件来完成本演练:

  • Visual Studio Tools for Office(Visual Studio 2008 专业版 和 Visual Studio Team System 的可选组件)。

  • Microsoft Office Word 2007。

默认情况下,Visual Studio Tools for Office 随列出的 Visual Studio 版本一起安装。若要检查它是否已安装,请参见安装 Visual Studio Tools for Office

创建新项目

第一步是创建一个 Word 外接程序项目。

创建新项目

Visual Studio 会将“我的配方智能标记”项目添加到“解决方案资源管理器”中。

配置项目

该项目需要引用智能标记 DLL,还需要使用正则表达式。

配置项目

  1. 在“项目”菜单上单击“添加引用”。

  2. 在“COM”选项卡上,选择“Microsoft Smart Tags 2.0 类型库”[Microsoft Smart Tags 2.0 Type Library],并单击“确定”。

  3. 在“解决方案资源管理器”中,右击 ThisDocument.vb (Visual Basic) 或 ThisDocument.cs (C#),然后单击“查看代码”。

  4. 将以下代码行添加到文件顶部。

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

创建智能标记

为了使智能标记能够找到并转换汤匙度量值,请向智能标记识别的术语列表中添加一个正则表达式,并创建一项在用户单击智能标记时可以使用的操作。

创建智能标记

  1. 使用下面的代码替换 ThisAddIn_Startup 类中的 ThisAddIn_Startup 事件处理程序。此代码创建一个表示 Visual Studio Tools for Office 智能标记的 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 标识通过为智能标记分配的正则表达式捕获的组。有关 Visual Studio Tools for Office 智能标记中的属性包和正则表达式的更多信息,请参见智能标记的结构

创建事件处理程序

  • 将下面的代码复制到 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 中,启用智能标记。

    有关更多信息,请参见如何:在 Word 和 Excel 中启用智能标记

  2. 按 F5 运行项目。

  3. 键入以汤匙为单位度量配料量的配方。

  4. 单击出现在识别到的字符串上方的智能标记图标,然后单击“Convert to ounces”(换算成盎司)。

  5. 确认是否在汤匙量后插入了等价的盎司量。

请参见

任务

如何:在 Word 和 Excel 中启用智能标记

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

如何:向 Excel 工作簿添加智能标记

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

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

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

概念

智能标记概述

智能标记的结构

修订记录

日期

修订

原因

2008 年 7 月

新增主题。

SP1 功能更改。