Comment : ajouter des balises actives aux classeurs Excel

Vous pouvez ajouter des balises actives à vos classeurs Microsoft Office Excel 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 classeurs. 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 Excel 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 de balises actives dans des personnalisations au niveau du document

  • Ajout de balises actives dans des compléments 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.

lien vers la vidéo Pour obtenir une version vidéo de cette rubrique, consultez Comment faire pour ajouter des balises actives à des classeurs Excel ? (page éventuellement en anglais).

Ajout de balises actives dans des personnalisations au niveau du document

Lorsque vous ajoutez une balise active par le biais d'une personnalisation au niveau du document, la balise active n'est reconnue que dans le classeur 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 ThisWorkbook.

L'exemple de code suivant montre comment créer une balise active qui reconnaît le mot sale et l'expression régulière [I|i]ssue\s\d{5,6}. Lorsque l'utilisateur tape vente ou une chaîne qui correspond à l'expression régulière (par exemple problème 12345), puis clique sur la balise active, l'emplacement de la cellule contenant le texte reconnu s'affiche. Pour exécuter ce code, ajoutez le code à la classe ThisWorkbook et appelez la méthode AddSmartTag à partir du gestionnaire d'événements ThisWorkbook_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.

WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Excel.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.Excel.SmartTag( _
    '    "www.microsoft.com/Demo#DemoSmartTag", _
    '    "Demonstration Smart Tag")

    ' Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale")
    smartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' 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.Excel.Action("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Excel.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.Excel.ActionEventArgs) _
    Handles DisplayAddress.BeforeCaptionShow

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

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

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

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub
private Microsoft.Office.Tools.Excel.Action displayAddress;

private void AddSmartTag()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Excel.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.Excel.SmartTag smartTagDemo =
        // new Microsoft.Office.Tools.Excel.SmartTag(
        //     "www.microsoft.com/Demo#DemoSmartTag",
        //     "Demonstration Smart Tag");

    // Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale");
    smartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

    // 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.Excel.Action("To be replaced");

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

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

    displayAddress.BeforeCaptionShow += new 
        Microsoft.Office.Tools.Excel.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    displayAddress.Click += new 
        Microsoft.Office.Tools.Excel.ActionClickEventHandler(
        DisplayAddress_Click);
}

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

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

void DisplayAddress_Click(object sender, 
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

Ajout de balises actives dans des compléments 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 classeur spécifique ou dans tous les classeurs ouverts. Les balises actives qui s'exécutent dans tous les classeurs ouverts sont appelées balises actives d'application.

Pour ajouter une balise active dans un classeur 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.Excel.Workbook pour le classeur pour lequel vous souhaitez héberger 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.Excel.Workbook.

L'exemple de code suivant montre comment créer une balise active qui reconnaît le mot sale et l'expression régulière [I|i]ssue\s\d{5,6}. Lorsque l'utilisateur tape vente ou une chaîne qui correspond à l'expression régulière (par exemple problème 12345), puis clique sur la balise active, l'emplacement de la cellule contenant le texte reconnu s'affiche. Pour exécuter ce code, ajoutez le code à la classe ThisAddIn, appelez la méthode AddSmartTagToWorkbook à partir du gestionnaire d'événements ThisAddIn_Startup et passez un Microsoft.Office.Interop.Excel.Workbook à AddSmartTagToWorkbook.

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.

WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTagToWorkbook(ByVal workbook As Excel.Workbook)
    ' Create a smart tag for .NET Framework 3.5 projects.
    ' Dim smartTagDemo As New  _
    '    Microsoft.Office.Tools.Excel.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 a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale")
    smartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' Create the action for .NET Framework 3.5 projects.
    ' displayAddress = New Microsoft.Office.Tools.Excel.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.Excel.Action() { _
            displayAddress}


    ' Get the host item for the workbook in .NET Framework 3.5 projects.
    ' Dim vstoWorkbook As Microsoft.Office.Tools.Excel.Workbook = _
    '  workbook.GetVstoObject()
    ' Get the host item for the workbook in .NET Framework 4 projects.
    Dim vstoWorkbook As Microsoft.Office.Tools.Excel.Workbook = _
        Globals.Factory.GetVstoObject(workbook)

    ' Add the smart tag to the active workbook.
    vstoWorkbook.VstoSmartTags.Add(smartTagDemo)
End Sub


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

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

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

End Sub

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

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub
private Microsoft.Office.Tools.Excel.Action displayAddress;

private void AddSmartTagToWorkbook(Excel.Workbook workbook)
{
    Microsoft.Office.Tools.Excel.SmartTag smartTagDemo =
        // Create a smart tag for .NET Framework 3.5 projects.
        // new Microsoft.Office.Tools.Excel.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 a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale");
    smartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

    // Create the action for .NET Framework 3.5 projects.
    // displayAddress = new Microsoft.Office.Tools.Excel.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.Excel.Action[] { displayAddress };

    // Get the host item for the workbook in .NET Framework 3.5 projects.
    // Microsoft.Office.Tools.Excel.Workbook vstoWorkbook =
    //    workbook.GetVstoObject();
    // Get the host item for the workbook in .NET Framework 4 projects.
    Microsoft.Office.Tools.Excel.Workbook vstoWorkbook =
        Globals.Factory.GetVstoObject(workbook);

    // Add the smart tag to the active workbook.
    vstoWorkbook.VstoSmartTags.Add(smartTagDemo);

    displayAddress.BeforeCaptionShow += new
        Microsoft.Office.Tools.Excel.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    displayAddress.Click += new
        Microsoft.Office.Tools.Excel.ActionClickEventHandler(
        DisplayAddress_Click);
}

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

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

void DisplayAddress_Click(object sender,
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

Pour ajouter une balise active qui fonctionne dans tous les classeurs 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 le mot sale et l'expression régulière [I|i]ssue\s\d{5,6}. Lorsque l'utilisateur tape vente ou une chaîne qui correspond à l'expression régulière (par exemple problème 12345), puis clique sur la balise active, l'emplacement de la cellule contenant le texte reconnu s'affiche. Pour exécuter ce code, ajoutez le code à 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.

WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action

Private Sub AddSmartTag()

    ' Create the smart tag for .NET Framework 4 projects.
    Dim smartTagDemo As Microsoft.Office.Tools.Excel.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.Excel.SmartTag( _
    '    "www.microsoft.com/Demo#DemoSmartTag", _
    '    "Demonstration Smart Tag")

    ' Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale")
    smartTagDemo.Expressions.Add( _
        New System.Text.RegularExpressions.Regex( _
        "[I|i]ssue\s\d{5,6}"))

    ' 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.Excel.Action("To be replaced")

    ' Add the action to the smart tag.
    smartTagDemo.Actions = New Microsoft.Office.Tools.Excel.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.Excel.ActionEventArgs) _
    Handles DisplayAddress.BeforeCaptionShow

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

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

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

    Dim smartTagAddress As String = e.Range.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("The recognized text '" & e.Text & _
            "' is at range " & smartTagAddress)
End Sub
private Microsoft.Office.Tools.Excel.Action displayAddress;

private void AddSmartTag()
{
    // Create the smart tag for .NET Framework 4 projects.
    Microsoft.Office.Tools.Excel.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.Excel.SmartTag smartTagDemo =
        // new Microsoft.Office.Tools.Excel.SmartTag(
        //     "www.microsoft.com/Demo#DemoSmartTag",
        //     "Demonstration Smart Tag");

    // Specify a term and an expression to recognize.
    smartTagDemo.Terms.Add("sale");
    smartTagDemo.Expressions.Add(
        new System.Text.RegularExpressions.Regex(
        @"[I|i]ssue\s\d{5,6}"));

    // 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.Excel.Action("To be replaced");

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

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

    displayAddress.BeforeCaptionShow += new 
        Microsoft.Office.Tools.Excel.BeforeCaptionShowEventHandler(
        DisplayAddress_BeforeCaptionShow);

    displayAddress.Click += new 
        Microsoft.Office.Tools.Excel.ActionClickEventHandler(
        DisplayAddress_Click);
}

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

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

void DisplayAddress_Click(object sender, 
    Microsoft.Office.Tools.Excel.ActionEventArgs e)
{
    string smartTagAddress = e.Range.get_Address(missing,
        missing, Excel.XlReferenceStyle.xlA1, missing, missing);
    System.Windows.Forms.MessageBox.Show("The recognized text '" + e.Text +
        "' is at range " + smartTagAddress);
}

Sécurité

Vous devez activer les balises actives dans Excel. 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 à des documents Word

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

Procédure : comment ajouter des balises actives à des classeurs Excel ? (page éventuellement en anglais)

Vue d'ensemble des balises actives

Développement de solutions Office