Condividi tramite


Procedura: aggiungere smart tag ai documenti di Word

Aggiornamento: Luglio 2008

Si applica a

Le informazioni contenute in questo argomento riguardano solo i progetti Visual Studio Tools per Office e le versioni di Microsoft Office specificati.

Progetti a livello di documento

  • Word 2003

  • Word 2007

Progetti a livello di applicazione

  • Word 2007

Per ulteriori informazioni, vedere la classe Funzionalità disponibili in base ai tipi di progetto e applicazione.

È possibile aggiungere smart tag ai documenti di Microsoft Office Word per consentire il riconoscimento del testo nonché l'accesso alle azioni relative ai termini riconosciuti.

A partire da Visual Studio 2008 Service Pack 1 (SP1) è possibile utilizzare componenti aggiuntivi a livello di applicazione per aggiungere smart tag in qualsiasi documento aperto. 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 ai documenti. Inoltre, l'ambito degli smart tag varia a seconda che il progetto sia a livello di documento o a livello di applicazione.

In questo argomento vengono descritte le attività seguenti:

  • Aggiunta di uno smart tag tramite una personalizzazione a livello di documento

  • Aggiunta di uno smart tag tramite un componente aggiuntivo 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 la classe Procedura: attivare gli smart tag in Word ed Excel.

Aggiunta di uno smart tag tramite una personalizzazione a livello di documento

Gli smart tag delle personalizzazioni a livello di documento sono riconosciuti solo nel documento associato 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 la classe Architettura degli smart tag.

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

Nell'esempio di codice seguente si crea uno smart tag che riconosce le parole term e recognize. Quando l'utente fa clic sullo smart tag, quest'ultimo visualizza le posizioni del carattere iniziale e di quello finale della parola riconosciuta. Per eseguire questo codice, aggiungerlo alla classe ThisDocument e quindi chiamare il metodo AddSmartTag dal gestore eventi ThisDocument_Startup.

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

Aggiunta di uno smart tag tramite un componente aggiuntivo a livello di applicazione

A partire da SP1 è possibile aggiungere uno smart tag mediante un componente aggiuntivo a livello di applicazione. È possibile specificare se lo smart tag deve funzionare soltanto in un documento specifico o in tutti i documenti aperti. In quest'ultimo caso si parla di smart tag a livello di applicazione.

Per aggiungere uno smart tag a un documento specifico

  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 la classe Architettura degli smart tag.

  2. Utilizzare il metodo GetVstoObject per creare un elemento host Document del documento 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.

    Nota:

    Se si utilizza un progetto creato prima di installare SP1, affinché sia possibile utilizzare il metodo GetVstoObject occorre prima modificare il progetto. Per ulteriori informazioni, vedere la classe 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 dell'elemento host Document.

Nell'esempio di codice seguente viene creato uno smart tag nel documento attivo che riconosce le parole term e recognize. Quando l'utente fa clic sullo smart tag, quest'ultimo visualizza le posizioni del carattere iniziale e di quello finale della parola riconosciuta. Per eseguire questo codice, aggiungerlo alla classe ThisAddIn e quindi chiamare il metodo AddSmartTagToActiveDocument dal gestore eventi ThisAddIn_Startup.

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

Per aggiungere uno smart tag che funziona in tutti i documenti aperti

  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 la classe Architettura degli smart tag.

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

    Nota:

    Se si utilizza un progetto creato prima di installare SP1, tale progetto deve essere modificato affinché generi la proprietà VstoSmartTags. Per ulteriori informazioni, vedere la classe Procedura: aggiungere smart tag a livello di applicazione in progetti creati prima di SP1.

Nell'esempio di codice seguente si crea uno smart tag che riconosce le parole term e recognize. Quando l'utente fa clic sullo smart tag, quest'ultimo visualizza le posizioni del carattere iniziale e di quello finale della parola riconosciuta. Per eseguire questo codice, aggiungerlo alla classe ThisAddIn e quindi chiamare il metodo AddSmartTag dal gestore eventi ThisAddIn_Startup.

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

Sicurezza

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

Vedere anche

Attività

Procedura: attivare gli smart tag in Word ed Excel

Procedura: aggiungere smart tag a cartelle di lavoro di Excel

Procedura: aggiungere smart tag a livello di applicazione in progetti creati prima di SP1

Procedura: creare smart tag con sistemi di riconoscimento personalizzati in Word

Procedura: creare smart tag con sistemi di riconoscimento personalizzati in Excel

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

Cenni preliminari sugli smart tag

Architettura degli smart tag

Architettura degli smart tag

Sviluppo di soluzioni Office

Cronologia delle modifiche

Date

History

Motivo

Luglio 2008

Aggiunte nuove procedure per componenti aggiuntivi a livello di applicazione.

Modifica di funzionalità in SP1.