Share via


方法 : Excel でカスタム レコグナイザを持つスマート タグを作成する

更新 : 2008 年 7 月

対象

このトピックの情報は、指定された Visual Studio Tools for Office プロジェクトおよび Microsoft Office のバージョンにのみ適用されます。

ドキュメント レベルのプロジェクト

  • Excel 2003

  • Excel 2007

  • Word 2003

  • Word 2007

アプリケーション レベルのプロジェクト

  • Excel 2007

  • Word 2007

詳細については、「アプリケーションおよびプロジェクトの種類別の使用可能な機能」を参照してください。

Microsoft Office Excel がブック内のスマート タグをどのように認識するかは、Microsoft.Office.Tools.Excel.SmartTag の派生クラスを作成し、Recognize メソッドをオーバーライドすることによって制御できます。

スマート タグを実行するには、Word または Excel でスマート タグを有効にしておく必要があります。詳細については、「方法 : Word および Excel でスマート タグを有効にする」を参照してください。

カスタム レコグナイザを持つスマート タグを Excel ブックに追加するには

  1. Excel 用のドキュメント レベルまたはアプリケーション レベルのプロジェクトを新しく作成します。詳細については、「方法 : Visual Studio Tools for Office プロジェクトを作成する」を参照してください。

    ms268704.alert_note(ja-jp,VS.90).gifメモ :

    アプリケーション レベルのプロジェクトを使用するには、Visual Studio 2008 Service Pack 1 (SP1) がインストールされている必要があります。

  2. [参照の追加] ダイアログ ボックスの [COM] タブで、[Microsoft Smart Tags 2.0 Type Library] への参照を追加します。

  3. プロジェクトにクラス ファイルを追加し、Microsoft.Office.Tools.Excel.SmartTag から継承するクラスを作成します。

  4. 新しいクラスで、スマート タグのアクションを作成します。アクションとは、スマート タグ メニューに表示される項目です。作成したクラスの Actions コレクションに Action 型のインスタンスを追加することで、アクションを作成します。

  5. Recognize メソッドをオーバーライドして、カスタム レコグナイザの独自の動作を実装します。独自に実装した Recognize では、Excel でスマート タグを認識させるために PersistTag メソッドを呼び出す必要があります。

  6. 作成したアクションの Click イベントに応答するイベント ハンドラを作成します。必要に応じて、BeforeCaptionShow イベントのイベント ハンドラも作成します。

  7. プロジェクト ブックのコード ファイルで、スマート タグのインスタンスを ThisWorkbook クラスの VstoSmartTags プロパティ (ドキュメント レベルのプロジェクトの場合) または ThisAddIn クラスの VstoSmartTags プロパティ (アプリケーション レベルのプロジェクトの場合) に追加します。

    ms268704.alert_note(ja-jp,VS.90).gifメモ :

    SP1 をインストールする前に作成したアプリケーション レベルのプロジェクトを使用している場合は、VstoSmartTags プロパティを生成するようにプロジェクトを変更する必要があります。詳細については、「方法 : SP1 より前に作成されたプロジェクトにアプリケーション レベルのスマート タグを追加する」を参照してください。

使用例

Excel ブックにカスタム スマート タグを作成するコード例を次に示します。この例では、Recognize メソッドをオーバーライドして、ワークシートのセル内で sales および organization という項目を認識するようにします。Recognize メソッドは、スマート タグのキー付きプロパティのコレクションに、キーと値の組み合わせを追加します。このメソッドは次に、スマート タグを認識して新しいスマート タグ プロパティを保存するために PersistTag メソッドを呼び出します。この例をテストするには、ブックの別々のセルに「sales」および「organization」という単語を入力してから、スマート タグ アクションを実行します。アクションの 1 つは認識された項目に対応するプロパティ値を表示し、もう 1 つのアクションはスマート タグの名前空間とキャプションを表示します。

Imports Microsoft.Office.Tools.Excel
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 cell text for the first instance of 
            ' the current smart tag term.
            Dim index As Integer = Me.CellText.IndexOf(Term, 0)

            If (index >= 0) Then

                ' 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(propertyBag)

                ' This implementation only finds the first instance
                ' of a smart tag term in the cell. 
                Exit For
            End If
        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.Excel;
using Microsoft.Office.Interop.SmartTag;

namespace Trin_ExcelDerivedSmartTags
{
    public class CustomSmartTag : SmartTag {

        // Declare Actions for this SmartTag
        Microsoft.Office.Tools.Excel.Action Action1 =
            new Microsoft.Office.Tools.Excel.Action("Display property value");
        Microsoft.Office.Tools.Excel.Action Action2 =
            new Microsoft.Office.Tools.Excel.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.Excel.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)
        {
            // Determine whether each smart tag term exists in 
            // the document text.
            foreach (string term in this.Terms)
            {
                // Search the cell text for the first instance of 
                // the current smart tag term.
                int index = this.CellText.IndexOf(term, 0);

                if (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(propertyBag);

                    // This implementation only finds the first instance
                    // of a smart tag term in the cell. 
                    break;
                }
            }
        }

        // This action displays the property value for the term.
        private void Action1_Click(object sender, 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, ActionEventArgs e)
        {
            MessageBox.Show("The current smart tag caption is '" + 
                this.Caption + "'. The current smart tag type is '" + 
                this.SmartTagType + "'.");
        }
    }
}

コードのコンパイル方法

  • [参照の追加] ダイアログ ボックスの [COM] タブで、プロジェクトの [Microsoft Smart Tags 2.0 Type Library] への参照を追加します。参照の ローカル コピー プロパティが false に設定されていることを確認します。このプロパティが true の場合、この参照は正しいプライマリ相互運用機能アセンブリではないので、Microsoft Office インストール メディアから正しいアセンブリをインストールする必要があります。詳細については、「方法 : Office のプライマリ相互運用機能アセンブリをインストールする」を参照してください。

  • CustomSmartTag という名前の新しいクラス ファイルにコード例を記述します。

  • C# では、プロジェクト名と一致するように名前空間を変更します。

  • クラス ファイルの先頭に、Microsoft.Office.Tools.Excel 名前空間および Microsoft.Office.Interop.SmartTag 名前空間に対する Imports ステートメント (Visual Basic の場合) または using ステートメント (C# の場合) を追加します。

  • プロジェクトの ThisWorkbook_Startup イベント ハンドラまたは ThisAddIn_Startup イベント ハンドラに、次のコードを追加します。このコードは、ブックにカスタム スマート タグを追加します。

    Me.VstoSmartTags.Add(New CustomSmartTag())
    
    this.VstoSmartTags.Add(new CustomSmartTag());
    

セキュリティ

Excel でスマート タグを有効にする必要があります。既定では、スマート タグは有効になっていません。詳細については、「方法 : Word および Excel でスマート タグを有効にする」を参照してください。

参照

処理手順

方法 : Word および Excel でスマート タグを有効にする

方法 : Word 文書にスマート タグを追加する

方法 : Excel ブックにスマート タグを追加する

方法 : Word でカスタム レコグナイザを持つスマート タグを作成する

チュートリアル : ドキュメント レベルのカスタマイズを使用したスマート タグの作成

チュートリアル : アプリケーション レベルのアドインを使用したスマート タグの作成

概念

スマート タグの概要

スマート タグのアーキテクチャ

Office UI のカスタマイズ

変更履歴

日付

履歴

理由

2008 年 7 月

アプリケーション レベルのアドインに関する新しい情報を追加

SP1 機能変更