Procedura: aggiungere smart tag a cartelle di lavoro di Excel

È possibile aggiungere smart tag alle cartelle di lavoro di Microsoft Office Excel per consentire il riconoscimento del testo nonché l'accesso alle azioni relative ai termini riconosciuti. Il codice che occorre scrivere per creare e configurare uno smart tag è lo stesso sia per i progetti a livello di documento sia per quelli a livello di applicazione. Tuttavia, esistono alcune differenze nel modo in cui gli smart tag vengono associati alle cartelle di lavoro. Inoltre, l'ambito degli smart tag varia a seconda che il progetto sia a livello di documento o a livello di applicazione.

Si applica a: le informazioni fornite in questo argomento sono valide per i progetti a livello di documento e di applicazione per Excel 2007. Per ulteriori informazioni, vedere Funzionalità disponibili in base ai tipi di progetto e applicazioni di Office.

In questo argomento vengono descritte le attività seguenti:

  • Aggiunta di smart tag nelle personalizzazioni a livello di documento

  • Aggiunta di smart tag nei componenti aggiuntivi a livello di applicazione

Per eseguire uno smart tag gli utenti finali devono avere gli smart tag attivati in Word o Excel. Per ulteriori informazioni, vedere Procedura: abilitare gli smart tag in Word ed Excel.

Collegamento a video Per una versione video di questo argomento, vedere How Do I: Add Smart Tags to Excel Workbooks (la pagina potrebbe essere in inglese).

Aggiunta di smart tag nelle personalizzazioni a livello di documento

Quando si aggiunge uno smart tag tramite una personalizzazione a livello di documento, lo smart tag viene riconosciuto soltanto nella cartella di lavoro associata alla personalizzazione.

Per aggiungere uno smart tag tramite una personalizzazione a livello di documento

  1. Creare un oggetto SmartTag e configurarlo per definire il comportamento dello smart tag:

    • Per specificare il testo da riconoscere, utilizzare la proprietà Terms o la proprietà Expressions.

    • Per definire le azioni su cui gli utenti possono fare clic nello smart tag, aggiungere uno o più oggetti Action alla proprietà Actions.

    Per ulteriori informazioni, vedere Architettura degli smart tag.

  2. Aggiungere l'oggetto SmartTag alla proprietà VstoSmartTags della classe ThisWorkbook.

Nell'esempio di codice seguente viene creato uno smart tag che riconosce la parola sale e l'espressione regolare [I|i]ssue\s\d{5,6}. Quando l'utente digita sale o una stringa che corrisponde all'espressione regolare (ad esempio issue 12345) e quindi fa clic sullo smart tag, quest'ultimo visualizza la posizione della cella del testo riconosciuto. Per eseguire questo codice, aggiungerlo alla classe ThisWorkbook e chiamare il metodo AddSmartTag dal gestore eventi ThisWorkbook_Startup.

Nota

L'esempio riportato di seguito funziona nei progetti rivolti a .NET Framework 4. Per utilizzare questo esempio nei progetti destinati a .NET Framework 3.5, vedere i commenti nel codice.

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);
}

Aggiunta di smart tag nei componenti aggiuntivi a livello di applicazione

Quando si aggiunge uno smart tag tramite un componente aggiuntivo a livello di applicazione, è possibile specificare se fare in modo che lo smart tag funzioni solo in una cartella di lavoro specifica o in tutte le cartelle di lavoro aperte. Gli smart tag eseguiti in tutte le cartelle di lavoro aperte sono noti come smart tag a livello di applicazione.

Per aggiungere uno smart tag a una cartella di lavoro specifica

  1. Creare un oggetto SmartTag e configurarlo per definire il comportamento dello smart tag:

    • Per specificare il testo da riconoscere, utilizzare la proprietà Terms o la proprietà Expressions.

    • Per definire le azioni su cui gli utenti possono fare clic nello smart tag, aggiungere uno o più oggetti Action alla proprietà Actions.

    Per ulteriori informazioni, vedere Architettura degli smart tag.

  2. Utilizzare il metodo GetVstoObject per creare un elemento host Microsoft.Office.Tools.Excel.Workbook della cartella di lavoro in cui si desidera ospitare lo smart tag. Per ulteriori informazioni sulla creazione di elementi host, vedere Estensione in fase di esecuzione di documenti di Word e di cartelle di lavoro di Excel in componenti aggiuntivi a livello di applicazione.

  3. Aggiungere l'oggetto SmartTag alla proprietà VstoSmartTags di Microsoft.Office.Tools.Excel.Workbook.

Nell'esempio di codice seguente viene creato uno smart tag che riconosce la parola sale e l'espressione regolare [I|i]ssue\s\d{5,6}. Quando l'utente digita sale o una stringa che corrisponde all'espressione regolare (ad esempio issue 12345) e quindi fa clic sullo smart tag, quest'ultimo visualizza la posizione della cella del testo riconosciuto. Per eseguire questo codice, aggiungerlo alla classe ThisAddIn, chiamare il metodo AddSmartTagToWorkbook dal gestore eventi ThisAddIn_Startup e passare un oggetto Microsoft.Office.Interop.Excel.Workbook a AddSmartTagToWorkbook.

Nota

L'esempio riportato di seguito funziona nei progetti rivolti a .NET Framework 4. Per utilizzare questo esempio nei progetti destinati a .NET Framework 3.5, vedere i commenti nel codice.

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);
}

Per aggiungere uno smart tag che funziona in tutte le cartelle di lavoro aperte

  1. Creare un oggetto SmartTag e configurarlo per definire il comportamento dello smart tag:

    • Per specificare il testo da riconoscere, utilizzare la proprietà Terms o la proprietà Expressions.

    • Per definire le azioni su cui gli utenti possono fare clic nello smart tag, aggiungere uno o più oggetti Action alla proprietà Actions.

    Per ulteriori informazioni, vedere Architettura degli smart tag.

  2. Aggiungere l'oggetto SmartTag alla proprietà VstoSmartTags della classe ThisAddIn.

Nell'esempio di codice seguente viene creato uno smart tag che riconosce la parola sale e l'espressione regolare [I|i]ssue\s\d{5,6}. Quando l'utente digita sale o una stringa che corrisponde all'espressione regolare (ad esempio issue 12345) e quindi fa clic sullo smart tag, quest'ultimo visualizza la posizione della cella del testo riconosciuto. Per eseguire questo codice, aggiungerlo alla classe ThisAddIn e chiamare il metodo AddSmartTag dal gestore eventi ThisAddIn_Startup.

Nota

L'esempio riportato di seguito funziona nei progetti rivolti a .NET Framework 4. Per utilizzare questo esempio nei progetti destinati a .NET Framework 3.5, vedere i commenti nel codice.

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);
}

Sicurezza

In Excel gli smart tag devono essere attivati esplicitamente, in quanto per impostazione predefinita non sono attivati. Per ulteriori informazioni, vedere Procedura: abilitare gli smart tag in Word ed Excel.

Vedere anche

Attività

Procedura: abilitare gli smart tag in Word ed Excel

Procedura: aggiungere smart tag ai documenti di Word

Procedura: creare smart tag con sistemi di riconoscimento personalizzati in Word e .NET Framework 3.5

Procedura: creare smart tag con sistemi di riconoscimento personalizzati in Excel e .NET Framework 3.5

Procedura dettagliata: creazione di uno smart tag tramite una personalizzazione a livello di documento

Procedura dettagliata: creazione di uno smart tag tramite un componente aggiuntivo a livello di applicazione

Concetti

Architettura degli smart tag

Altre risorse

Ricerca per categorie: aggiungere smart tag a cartelle di lavoro di Excel

Cenni preliminari sugli smart tag

Sviluppo di soluzioni Office