Gewusst wie: Hinzufügen von Smarttags zu Word-Dokumenten

Sie können Smarttags für Microsoft Office Word-Dokumente hinzufügen, um Text zu erkennen und Benutzern Zugriff auf Aktionen zu gewähren, die mit den erkannten Begriffen verknüpft sind. Der Code, den Sie zum Erstellen und Konfigurieren eines Smarttags schreiben, ist für Projekte auf Dokumentebene und Projekte auf Anwendungsebene gleich. Allerdings gibt es einige Unterschiede bei der Verknüpfung eines Smarttags mit Dokumenten. Smarttags haben in Projekten auf Dokumentebene und in Projekten auf Anwendungsebene auch einen unterschiedlichen Gültigkeitsbereich.

Betrifft: Die Informationen in diesem Thema betreffen Projekte auf Dokument- und auf Anwendungsebene für Word 2007. Weitere Informationen finden Sie unter Verfügbare Funktionen nach Office-Anwendung und Projekttyp.

In diesem Thema werden die folgenden Aufgaben erläutert:

  • Hinzufügen eines Smarttags mit einer Anpassung auf Dokumentebene

  • Hinzufügen eines Smarttags mit einem Add-In auf Anwendungsebene

Wenn Endbenutzer ein Smarttag ausführen möchten, müssen Smarttags in Word oder Excel aktiviert sein. Weitere Informationen hierzu finden Sie unter Gewusst wie: Aktivieren von Smarttags in Word und Excel.

Hinzufügen eines Smarttags mit einer Anpassung auf Dokumentebene

Smarttags in Anpassungen auf Dokumentebene werden nur in dem Dokument erkannt, das der Anpassung zugeordnet ist.

So fügen Sie ein Smarttag mit einer Anpassung auf Dokumentebene hinzu

  1. Erstellen Sie ein SmartTag-Objekt, und konfigurieren Sie dieses Objekt, um das Verhalten des Smarttags zu definieren:

    • Um den Text anzugeben, der erkannt werden soll, verwenden Sie die Terms-Eigenschaft oder die Expressions-Eigenschaft.

    • Um die Aktionen im Smarttag zu definieren, auf die der Benutzer klicken kann, fügen Sie ein oder mehrere Action-Objekte der Actions-Eigenschaft hinzu.

    Weitere Informationen hierzu finden Sie unter Smarttagarchitektur.

  2. Fügen Sie das SmartTag der VstoSmartTags-Eigenschaft der ThisDocument-Klasse hinzu.

Im folgenden Codebeispiel wird ein Smarttag erstellt, das die Wörter term und recognize erkennt. Wenn der Benutzer auf das Smarttag klickt, werden die Positionen von Anfangs- und Endzeichen des erkannten Worts angezeigt. Um diesen Code auszuführen, fügen Sie den Code der ThisDocument-Klasse hinzu, und rufen Sie die AddSmartTag-Methode des ThisDocument_Startup-Ereignishandlers auf.

Tipp

Das folgende Beispiel funktioniert in Projekten mit der Zielversion .NET Framework 4. Weitere Informationen zur Verwendung dieses Beispiels in Projekten mit der Zielversion .NET Framework 3.5 finden Sie in den Kommentaren im Code.

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

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Word.SmartTag = Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag")

    ' For .NET Framework 3.5 projects, use the following code to create the smart tag.
    ' 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 for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced")

    ' For .NET Framework 3.5 projects, use the following code to 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 DisplayAddress_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()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag");

    // For .NET Framework 3.5 projects, use the following code to create the smart tag.
    // 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 for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced"); 

    // For .NET Framework 3.5 projects, use the following code to 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());
}

Hinzufügen eines Smarttags mit einem Add-In auf Anwendungsebene

Wenn Sie ein Smarttag mit einem Add-In auf Anwendungsebene hinzufügen, können Sie angeben, ob das Smarttag nur in einem bestimmten Dokument funktionieren soll oder in allen geöffneten Dokumenten (auch als Smarttag auf Anwendungsebene bezeichnet).

So fügen Sie ein Smarttag einem bestimmten Dokument hinzu

  1. Erstellen Sie ein SmartTag-Objekt, und konfigurieren Sie dieses Objekt, um das Verhalten des Smarttags zu definieren:

    • Um den Text anzugeben, der erkannt werden soll, verwenden Sie die Terms-Eigenschaft oder die Expressions-Eigenschaft.

    • Um die Aktionen im Smarttag zu definieren, auf die der Benutzer klicken kann, fügen Sie ein oder mehrere Action-Objekte der Actions-Eigenschaft hinzu.

    Weitere Informationen finden Sie unter Smarttagarchitektur.

  2. Verwenden Sie die GetVstoObject-Methode, um ein Microsoft.Office.Tools.Word.Document-Hostelement für das Dokument zu erstellen, das das Smarttag hosten soll. Weitere Informationen zum Erstellen von Hostelementen finden Sie unter Erweitern von Word-Dokumenten und Excel-Arbeitsmappen in Add-Ins auf Anwendungsebene zur Laufzeit.

  3. Fügen Sie SmartTag der VstoSmartTags-Eigenschaft von Microsoft.Office.Tools.Word.Document hinzu.

Im folgenden Codebeispiel wird ein Smarttag im aktiven Dokument erstellt, das die Wörter term und recognize erkennt. Wenn der Benutzer auf das Smarttag klickt, werden die Positionen von Anfangs- und Endzeichen des erkannten Worts angezeigt. Um diesen Code auszuführen, fügen Sie den Code zur ThisAddIn-Klasse hinzu, rufen Sie die AddSmartTagToDocument-Methode des ThisAddIn_Startup-Ereignishandlers auf, und übergeben Sie eine Microsoft.Office.Interop.Word.Document an AddSmartTagToDocument.

Tipp

Das folgende Beispiel funktioniert in Projekten mit der Zielversion .NET Framework 4. Weitere Informationen zur Verwendung dieses Beispiels in Projekten mit der Zielversion .NET Framework 3.5 finden Sie in den Kommentaren im Code.

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

Private Sub AddSmartTagToDocument(ByVal document As Word.Document)
    ' Create a smart tag for .NET Framework 3.5 projects.
    ' Dim smartTagDemo As New  _
    '    Microsoft.Office.Tools.Word.SmartTag( _
    '    "www.microsoft.com/Demo#DemoSmartTag", _
    '    "Demonstration Smart Tag")
    ' Create a smart tag for .NET Framework 4 projects.
    Dim  smartTagDemo As SmartTag = Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag")

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

    ' Create the action for .NET Framework 3.5 projects.
    ' displayAddress = New Microsoft.Office.Tools.Word.Action("To be replaced")
    ' Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced")

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

    ' Get a Document host item for .NET Framework 3.5
    ' Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
    ' document.GetVstoObject()
    ' Get a Document host item for .NET Framework 4
    Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
        Globals.Factory.GetVstoObject(document)

    ' Add the smart tag to the document.
    vstoDocument.VstoSmartTags.Add(smartTagDemo)
End Sub

Private Sub DisplayAddress_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 AddSmartTagToDocument(Word.Document document)
{
    Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        // Create a smart tag for .NET Framework 3.5 projects.
        //    new Microsoft.Office.Tools.Word.SmartTag(
        //    "www.microsoft.com/Demo#DemoSmartTag",
        //    "Demonstration Smart Tag");
        // Create a smart tag for .NET Framework 4 projects.
        Globals.Factory.CreateSmartTag(
            "www.microsoft.com/Demo#DemoSmartTag",
            "Demonstration Smart Tag");

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

    // Create the action for .NET Framework 3.5 projects.
    // displayAddress = new Microsoft.Office.Tools.Word.Action("To be replaced");
    // Create the action for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced");

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

    // Get a Document host item for .NET Framework 3.5
    // Microsoft.Office.Tools.Word.Document vstoDocument =
    //    document.GetVstoObject();
    // Get a Document host item for .NET Framework 3.5
    Microsoft.Office.Tools.Word.Document vstoDocument =
        Globals.Factory.GetVstoObject(document);
    // Add the smart tag to the document
    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());
}

So fügen Sie ein Smarttag hinzu, das in allen geöffneten Dokumenten funktioniert

  1. Erstellen Sie ein SmartTag-Objekt, und konfigurieren Sie dieses Objekt, um das Verhalten des Smarttags zu definieren:

    • Um den Text anzugeben, der erkannt werden soll, verwenden Sie die Terms-Eigenschaft oder die Expressions-Eigenschaft.

    • Um die Aktionen im Smarttag zu definieren, auf die der Benutzer klicken kann, fügen Sie ein oder mehrere Action-Objekte der Actions-Eigenschaft hinzu.

    Weitere Informationen hierzu finden Sie unter Smarttagarchitektur.

  2. Fügen Sie das SmartTag der VstoSmartTags-Eigenschaft der ThisAddIn-Klasse hinzu.

Im folgenden Codebeispiel wird ein Smarttag erstellt, das die Wörter term und recognize erkennt. Wenn der Benutzer auf das Smarttag klickt, werden die Positionen von Anfangs- und Endzeichen des erkannten Worts angezeigt. Um diesen Code auszuführen, fügen Sie den Code der ThisAddIn-Klasse hinzu, und rufen Sie die AddSmartTag-Methode des ThisAddIn_Startup-Ereignishandlers auf.

Tipp

Das folgende Beispiel funktioniert in Projekten mit der Zielversion .NET Framework 4. Weitere Informationen zur Verwendung dieses Beispiels in Projekten mit der Zielversion .NET Framework 3.5 finden Sie in den Kommentaren im Code.

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

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Word.SmartTag = Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag")

    ' For .NET Framework 3.5 projects, use the following code to create the smart tag.
    ' 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 for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced")

    ' For .NET Framework 3.5 projects, use the following code to 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 DisplayAddress_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()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Word.SmartTag smartTagDemo =
        Globals.Factory.CreateSmartTag(
        "www.microsoft.com/Demo#DemoSmartTag",
        "Demonstration Smart Tag");

    // For .NET Framework 3.5 projects, use the following code to create the smart tag.
    // 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 for .NET Framework 4 projects.
    displayAddress = Globals.Factory.CreateAction("To be replaced"); 

    // For .NET Framework 3.5 projects, use the following code to 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());
}

Sicherheit

Sie müssen Smarttags in Word aktivieren. Standardmäßig werden Smarttags nicht aktiviert. Weitere Informationen finden Sie unter Gewusst wie: Aktivieren von Smarttags in Word und Excel.

Siehe auch

Aufgaben

Gewusst wie: Aktivieren von Smarttags in Word und Excel

Gewusst wie: Hinzufügen von Smarttags zu Excel-Arbeitsmappen

Gewusst wie: Erstellen von Smarttags mit benutzerdefinierten Erkennungen in Word und .NET Framework 3.5

Gewusst wie: Erstellen von Smarttags mit benutzerdefinierten Erkennungen in Excel und .NET Framework 3.5

Exemplarische Vorgehensweise: Erstellen eines Smarttags mit einer Anpassung auf Dokumentebene

Exemplarische Vorgehensweise: Erstellen eines Smarttags mit einem Add-In auf Anwendungsebene

Konzepte

Smarttagarchitektur

Weitere Ressourcen

Übersicht über Smarttags

Entwickeln von Office-Projektmappen