Comment : ajouter des balises actives à des documents Word

Vous pouvez ajouter des balises actives à vos documents Microsoft Office Word afin de reconnaître le texte et de permettre à l'utilisateur d'accéder aux actions en rapport avec les termes reconnus. Le code que vous écrivez pour créer et configurer une balise active est le même pour les projets au niveau du document et de l'application, mais il existe quelques différences dans la façon d'associer une balise active à des documents. Les balises actives ont également une portée différente selon qu'elles se trouvent dans un projet au niveau du document ou de l'application.

S'applique à : Les informations contenues dans cette rubrique s'appliquent aux projets de niveau document et de niveau application pour Word 2007. Pour en savoir plus, consultez Fonctionnalités disponibles par type d'application et de projet Office.

Cette rubrique décrit les tâches suivantes :

  • Ajout d'une balise active à l'aide d'une personnalisation au niveau du document

  • Ajout d'une balise active à l'aide d'un complément d'application

Pour exécuter une balise active, les balises actives doivent être activées dans Word ou Excel. Pour plus d'informations, consultez Comment : activer des balises actives dans Word et Excel.

Ajout d'une balise active à l'aide d'une personnalisation au niveau du document

Les balises actives des personnalisations au niveau du document ne sont reconnues que dans le document associé à la personnalisation.

Pour ajouter une balise active à l'aide d'une personnalisation au niveau du document

  1. Créez un objet SmartTag et configurez-le de façon à définir le comportement de la balise active :

    • Pour spécifier le texte que vous souhaitez reconnaître, utilisez les propriétés Terms ou Expressions.

    • Pour définir les actions que les utilisateurs peuvent déclencher en cliquant sur la balise active, ajoutez un ou plusieurs objets Action à la propriété Actions.

    Pour plus d'informations, consultez Architecture des balises actives.

  2. Ajoutez SmartTag à la propriété VstoSmartTags de la classe ThisDocument.

L'exemple de code suivant montre comment créer une balise active qui reconnaît les mots term et recognize. Lorsque l'utilisateur clique sur la balise active, les positions des caractères de début et de fin du mot reconnu s'affichent. Pour exécuter ce code, ajoutez-le à la classe ThisDocument et appelez la méthode AddSmartTag à partir du gestionnaire d'événements ThisDocument_Startup.

Notes

L'exemple suivant fonctionne dans les projets qui ciblent .NET Framework 4. Pour utiliser cet exemple dans des projets qui ciblent .NET Framework 3.5, consultez les commentaires du 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());
}

Ajout d'une balise active à l'aide d'un complément d'application

Lorsque vous ajoutez une balise active à l'aide d'un complément d'application, vous pouvez spécifier si la balise active doit fonctionner uniquement dans un document spécifique ou dans tous les documents ouverts (également appelée balise active d'application).

Pour ajouter une balise active dans un document spécifique

  1. Créez un objet SmartTag et configurez-le de façon à définir le comportement de la balise active :

    • Pour spécifier le texte que vous souhaitez reconnaître, utilisez les propriétés Terms ou Expressions.

    • Pour définir les actions que les utilisateurs peuvent déclencher en cliquant sur la balise active, ajoutez un ou plusieurs objets Action à la propriété Actions.

    Pour plus d'informations, consultez Architecture des balises actives.

  2. Pour créer un élément hôte Microsoft.Office.Tools.Word.Document pour le document qui hébergera la balise active, utilisez la méthode GetVstoObject. Pour plus d'informations sur la création d'éléments hôtes, consultez Extension de documents Word et de classeurs Excel dans des compléments d'application au moment de l'exécution..

  3. Ajoutez SmartTag à la propriété VstoSmartTags du Microsoft.Office.Tools.Word.Document.

L'exemple de code suivant montre comment créer, dans le document ouvert, une balise active qui reconnaît les mots term et recognize. Lorsque l'utilisateur clique sur la balise active, les positions des caractères de début et de fin du mot reconnu s'affichent. Pour exécuter ce code, ajoutez le code à la classe ThisAddIn, appelez la méthode AddSmartTagToDocument à partir du gestionnaire d'événements ThisAddIn_Startup et passez un Microsoft.Office.Interop.Word.Document à AddSmartTagToDocument.

Notes

L'exemple suivant fonctionne dans les projets qui ciblent .NET Framework 4. Pour utiliser cet exemple dans des projets qui ciblent .NET Framework 3.5, consultez les commentaires du 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());
}

Pour ajouter une balise active qui fonctionne dans tous les documents ouverts

  1. Créez un objet SmartTag et configurez-le de façon à définir le comportement de la balise active :

    • Pour spécifier le texte que vous souhaitez reconnaître, utilisez les propriétés Terms ou Expressions.

    • Pour définir les actions que les utilisateurs peuvent déclencher en cliquant sur la balise active, ajoutez un ou plusieurs objets Action à la propriété Actions.

    Pour plus d'informations, consultez Architecture des balises actives.

  2. Ajoutez SmartTag à la propriété VstoSmartTags de la classe ThisAddIn.

L'exemple de code suivant montre comment créer une balise active qui reconnaît les mots term et recognize. Lorsque l'utilisateur clique sur la balise active, les positions des caractères de début et de fin du mot reconnu s'affichent. Pour exécuter ce code, ajoutez-le à la classe ThisAddIn et appelez la méthode AddSmartTag à partir du gestionnaire d'événements ThisAddIn_Startup.

Notes

L'exemple suivant fonctionne dans les projets qui ciblent .NET Framework 4. Pour utiliser cet exemple dans des projets qui ciblent .NET Framework 3.5, consultez les commentaires du 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());
}

Sécurité

Vous devez activer les balises actives dans Word. Par défaut, elles sont désactivées. Pour plus d'informations, consultez Comment : activer des balises actives dans Word et Excel.

Voir aussi

Tâches

Comment : activer des balises actives dans Word et Excel

Comment : ajouter des balises actives aux classeurs Excel

Comment : créer des balises actives avec des modules de reconnaissance personnalisés dans Word et dans .NET Framework 3.5

Comment : créer des balises actives avec des modules de reconnaissance personnalisés dans Excel et dans .NET Framework 3.5

Procédure pas à pas : création d'une balise active à l'aide d'une personnalisation au niveau du document

Procédure pas à pas : création d'une balise active à l'aide d'un complément d'application

Concepts

Architecture des balises actives

Autres ressources

Vue d'ensemble des balises actives

Développement de solutions Office