次の方法で共有


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

更新 : 2008 年 7 月

対象

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

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

  • Word 2003

  • Word 2007

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

  • Word 2007

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

Microsoft Office Word 文書にテキストを認識するスマート タグを追加し、認識された項目に対してユーザーがアクションを実行できるようにすることができます。

Visual Studio 2008 Service Pack 1 (SP1) では、アプリケーション レベルのアドインを使用して、スマート タグを任意の開いている文書に追加できます。スマート タグを作成および構成するためのコードはドキュメント レベルのプロジェクトとアプリケーション レベルのプロジェクトで同じですが、スマート タグを文書に関連付ける方法が異なります。また、ドキュメント レベルのプロジェクトとアプリケーション レベルのプロジェクトとで、スマート タグのスコープが異なります。

このトピックでは、次のタスクについて説明します。

  • ドキュメント レベルのカスタマイズを使用したスマート タグの追加

  • アプリケーション レベルのアドインを使用したスマート タグの追加

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

ドキュメント レベルのカスタマイズを使用したスマート タグの追加

ドキュメント レベルのカスタマイズによるスマート タグは、カスタマイズに関連がある文書内でのみ認識されます。

ドキュメント レベルのカスタマイズを使用してスマート タグを追加するには

  1. SmartTag オブジェクトを作成し、このオブジェクトを構成してスマート タグの動作を定義します。

    • 認識するテキストを指定するには、Terms プロパティまたは Expressions プロパティを使用します。

    • スマート タグ上でユーザーがクリックできるアクションを定義するには、1 つまたは複数の Action オブジェクトを Actions プロパティに追加します。

    詳細については、「スマート タグのアーキテクチャ」を参照してください。

  2. SmartTag を ThisDocument クラスの VstoSmartTags プロパティに追加します。

term および recognize という単語を認識するスマート タグを作成する方法を次のコード例に示します。ユーザーがスマート タグをクリックすると、認識されたテキストの先頭および末尾の文字の位置が表示されます。このコードを実行するには、コードを ThisDocument クラスに追加し、ThisDocument_Startup イベント ハンドラから AddSmartTag メソッドを呼び出します。

Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action

Private Sub AddSmartTag()
    Dim smartTagDemo As New  _
        Microsoft.Office.Tools.Word.SmartTag( _
        "www.microsoft.com/Demo#DemoSmartTag", _
        "Demonstration Smart Tag")

    ' Specify the terms to recognize.
    smartTagDemo.Terms.Add("term")
    smartTagDemo.Terms.Add("recognize")

    ' Create the action.
    displayAddress = New Microsoft.Office.Tools.Word.Action("To be replaced")

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

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

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

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

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

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

    Dim termStart As Integer = e.Range.Start
    Dim termEnd As Integer = e.Range.End
    MsgBox("The recognized text '" & e.Text & _
            "' begins at position " & termStart & _
            " and ends at position " & termEnd)
End Sub
private Microsoft.Office.Tools.Word.Action displayAddress;

private void AddSmartTag()
{
    Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        new Microsoft.Office.Tools.Word.SmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag");

    // Specify the terms to recognize.
    smartTagDemo.Terms.Add("term");
    smartTagDemo.Terms.Add("recognize");

    // Create the action.
    displayAddress = new Microsoft.Office.Tools.Word.Action("To be replaced");

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

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

    displayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
        displayAddress_BeforeCaptionShow);

    displayAddress.Click += new
        Microsoft.Office.Tools.Word.ActionClickEventHandler(
        displayAddress_Click);
}

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

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

void displayAddress_Click(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    int termStart = e.Range.Start;
    int termEnd = e.Range.End;
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' begins at position " + termStart.ToString() +
        " and ends at position " + termEnd.ToString());
}

アプリケーション レベルのアドインを使用したスマート タグの追加

SP1 では、アプリケーション レベルのアドインを使用してスマート タグを追加できるようになりました。スマート タグが特定の文書内でのみ動作するように設定するか、またはすべての開いている文書内で動作する (アプリケーション レベルのスマート タグと呼ばれます) ように設定するかを指定できます。

特定の文書にスマート タグを追加するには

  1. SmartTag オブジェクトを作成し、このオブジェクトを構成してスマート タグの動作を定義します。

    • 認識するテキストを指定するには、Terms プロパティまたは Expressions プロパティを使用します。

    • スマート タグ上でユーザーがクリックできるアクションを定義するには、1 つまたは複数の Action オブジェクトを Actions プロパティに追加します。

    詳細については、「スマート タグのアーキテクチャ」を参照してください。

  2. GetVstoObject メソッドを使用して、スマート タグをホストする文書の Document ホスト項目を作成します。ホスト項目の作成の詳細については、「アプリケーション レベルのアドインにおける実行時の Word 文書や Excel ブックの拡張」を参照してください。

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

    SP1 をインストールする前に作成したプロジェクトを使用している場合は、GetVstoObject メソッドを使用する前にプロジェクトを変更する必要があります。詳細については、「アプリケーション レベルのアドインにおける実行時の Word 文書や Excel ブックの拡張」を参照してください。

  3. SmartTagDocumentVstoSmartTags プロパティに追加します。

term と recognize という単語を認識するスマート タグをアクティブな文書に作成する方法を次のコード例に示します。ユーザーがスマート タグをクリックすると、認識されたテキストの先頭および末尾の文字の位置が表示されます。このコードを実行するには、コードを ThisAddIn クラスに追加し、ThisAddIn_Startup イベント ハンドラから AddSmartTagToActiveDocument メソッドを呼び出します。

Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action

Private Sub AddSmartTagToActiveDocument()
    Dim smartTagDemo As New  _
        Microsoft.Office.Tools.Word.SmartTag( _
        "www.microsoft.com/Demo#DemoSmartTag", _
        "Demonstration Smart Tag")

    ' Specify the terms to recognize.
    smartTagDemo.Terms.Add("term")
    smartTagDemo.Terms.Add("recognize")

    ' Create the action.
    displayAddress = New Microsoft.Office.Tools.Word.Action("To be replaced")

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

    ' Get a Document host item, and add the smart tag to the document.
    Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
        Me.Application.ActiveDocument.GetVstoObject()
    vstoDocument.VstoSmartTags.Add(smartTagDemo)
End Sub

Private Sub OpenMessageBox_BeforeCaptionShow(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
    Handles displayAddress.BeforeCaptionShow

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

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

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

    Dim termStart As Integer = e.Range.Start
    Dim termEnd As Integer = e.Range.End
    MsgBox("The recognized text '" & e.Text & _
            "' begins at position " & termStart & _
            " and ends at position " & termEnd)
End Sub
private Microsoft.Office.Tools.Word.Action displayAddress;

private void AddSmartTagToActiveDocument()
{
    Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        new Microsoft.Office.Tools.Word.SmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag");

    // Specify the terms to recognize.
    smartTagDemo.Terms.Add("term");
    smartTagDemo.Terms.Add("recognize");

    // Create the action.
    displayAddress = new Microsoft.Office.Tools.Word.Action("To be replaced");

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

    // Add the smart tag to the document.
    Microsoft.Office.Tools.Word.Document vstoDocument =
        this.Application.ActiveDocument.GetVstoObject();
    vstoDocument.VstoSmartTags.Add(smartTagDemo);

    displayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
        displayAddress_BeforeCaptionShow);

    displayAddress.Click += new
        Microsoft.Office.Tools.Word.ActionClickEventHandler(
        displayAddress_Click);
}

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

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

void displayAddress_Click(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    int termStart = e.Range.Start;
    int termEnd = e.Range.End;
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' begins at position " + termStart.ToString() +
        " and ends at position " + termEnd.ToString());
}

すべての開いている文書で動作するスマート タグを追加するには

  1. SmartTag オブジェクトを作成し、このオブジェクトを構成してスマート タグの動作を定義します。

    • 認識するテキストを指定するには、Terms プロパティまたは Expressions プロパティを使用します。

    • スマート タグ上でユーザーがクリックできるアクションを定義するには、1 つまたは複数の Action オブジェクトを Actions プロパティに追加します。

    詳細については、「スマート タグのアーキテクチャ」を参照してください。

  2. SmartTag を ThisAddIn クラスの VstoSmartTags プロパティに追加します。

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

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

term および recognize という単語を認識するスマート タグを作成する方法を次のコード例に示します。ユーザーがスマート タグをクリックすると、認識されたテキストの先頭および末尾の文字の位置が表示されます。このコードを実行するには、コードを ThisAddIn クラスに追加し、ThisAddIn_Startup イベント ハンドラから AddSmartTag メソッドを呼び出します。

Private WithEvents displayAddress As Microsoft.Office.Tools.Word.Action

Private Sub AddSmartTag()
    Dim smartTagDemo As New  _
        Microsoft.Office.Tools.Word.SmartTag( _
        "www.microsoft.com/Demo#DemoSmartTag", _
        "Demonstration Smart Tag")

    ' Specify the terms to recognize.
    smartTagDemo.Terms.Add("term")
    smartTagDemo.Terms.Add("recognize")

    ' Create the action.
    displayAddress = New Microsoft.Office.Tools.Word.Action("To be replaced")

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

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

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

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

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

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

    Dim termStart As Integer = e.Range.Start
    Dim termEnd As Integer = e.Range.End
    MsgBox("The recognized text '" & e.Text & _
            "' begins at position " & termStart & _
            " and ends at position " & termEnd)
End Sub
private Microsoft.Office.Tools.Word.Action displayAddress;

private void AddSmartTag()
{
    Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        new Microsoft.Office.Tools.Word.SmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag");

    // Specify the terms to recognize.
    smartTagDemo.Terms.Add("term");
    smartTagDemo.Terms.Add("recognize");

    // Create the action.
    displayAddress = new Microsoft.Office.Tools.Word.Action("To be replaced");

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

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

    displayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
        displayAddress_BeforeCaptionShow);

    displayAddress.Click += new
        Microsoft.Office.Tools.Word.ActionClickEventHandler(
        displayAddress_Click);
}

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

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

void displayAddress_Click(object sender,
    Microsoft.Office.Tools.Word.ActionEventArgs e)
{
    int termStart = e.Range.Start;
    int termEnd = e.Range.End;
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' begins at position " + termStart.ToString() +
        " and ends at position " + termEnd.ToString());
}

セキュリティ

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

参照

処理手順

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

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

方法 : SP1 より前に作成されたプロジェクトにアプリケーション レベルのスマート タグを追加する

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

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

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

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

概念

スマート タグの概要

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

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

Office ソリューションの開発

変更履歴

[日付]

[履歴]

原因

2008 年 7 月

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

SP1 機能変更