Procedura: aggiungere smart tag a cartelle di lavoro di Excel
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
Progetti a livello di applicazione
Per ulteriori informazioni, vedere la classe Funzionalità disponibili in base ai tipi di progetto e applicazione. |
È 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.
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 la classe Procedura: attivare gli smart tag in Word ed Excel.
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
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.
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 quindi chiamare il metodo AddSmartTag dal gestore eventi ThisWorkbook_Startup.
WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action
Private Sub AddSmartTag()
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.
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 OpenMessageBox_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()
{
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.
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
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 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
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.
Utilizzare il metodo GetVstoObject per creare un elemento host 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.
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.
Aggiungere l'oggetto SmartTag alla proprietà VstoSmartTags dell'elemento host 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 e quindi chiamare il metodo AddSmartTagToActiveWorkbook dal gestore eventi ThisAddIn_Startup.
WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action
Private Sub AddSmartTagToActiveWorkbook()
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.
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 to the active workbook.
Dim vstoWorkbook As Microsoft.Office.Tools.Excel.Workbook = _
Me.Application.ActiveWorkbook.GetVstoObject()
vstoWorkbook.VstoSmartTags.Add(smartTagDemo)
End Sub
Private Sub OpenMessageBox_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 AddSmartTagToActiveWorkbook()
{
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.
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 to the active workbook.
Microsoft.Office.Tools.Excel.Workbook vstoWorkbook =
this.Application.ActiveWorkbook.GetVstoObject();
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
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.
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 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 quindi chiamare il metodo AddSmartTag dal gestore eventi ThisAddIn_Startup.
WithEvents displayAddress As Microsoft.Office.Tools.Excel.Action
Private Sub AddSmartTag()
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.
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 OpenMessageBox_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()
{
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.
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 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 ai documenti di Word
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
Concetti
Cenni preliminari sugli smart tag
Cronologia delle modifiche
Date |
History |
Motivo |
---|---|---|
Luglio 2008 |
Aggiunte nuove procedure per componenti aggiuntivi a livello di applicazione. |
Modifica di funzionalità in SP1. |