Condividi tramite


Metodo ApplicationFactory.GetVstoObject (_Worksheet)

Restituisce un elemento host Microsoft.Office.Tools.Excel.Worksheet che estende la funzionalità dell'oggetto cartella di lavoro nativo specificato.

Spazio dei nomi:  Microsoft.Office.Tools.Excel
Assembly:   Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)
  Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

Sintassi

'Dichiarazione
Function GetVstoObject ( _
    worksheet As _Worksheet _
) As Worksheet
Worksheet GetVstoObject(
    _Worksheet worksheet
)

Parametri

Valore restituito

Tipo: Microsoft.Office.Tools.Excel.Worksheet
Elemento host che estende la funzionalità dell'oggetto foglio di lavoro nativo.

Note

Chiamare questo metodo in un componente aggiuntivo a livello di applicazione per personalizzare qualsiasi foglio di lavoro aperto in Excel. Questo metodo genera un nuovo oggetto Microsoft.Office.Tools.Excel.Worksheet se un oggetto di questo tipo non è già stato generato. Le chiamate successive a questo metodo restituiscono l'istanza memorizzata nella cache dell'oggetto Microsoft.Office.Tools.Excel.Worksheet esistente. Per ulteriori informazioni, 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

Il parametro worksheet è di tipo Microsoft.Office.Interop.Excel._Worksheet, che è l'interfaccia padre di Microsoft.Office.Interop.Excel.Worksheet.Pertanto, questo metodo può accettare oggetti di entrambi i tipi: Microsoft.Office.Interop.Excel._Worksheet e Microsoft.Office.Interop.Excel.Worksheet.In genere, quando si fa riferimento a un foglio di lavoro di Excel, si utilizza un oggetto Microsoft.Office.Interop.Excel.Worksheet.

Esempi

Nell'esempio di codice riportato di seguito viene creato un elemento host Microsoft.Office.Tools.Excel.Worksheet per ogni oggetto Microsoft.Office.Interop.Excel.Workbook che presenta un elemento host. Per utilizzare questo codice, eseguirlo dalla classe ThisAddIn in un progetto di componente aggiuntivo di Excel destinato a .NET Framework 4 o .NET Framework 4.5.

Private Sub Application_WorkbookBeforeSave( _
    ByVal Wb As Microsoft.Office.Interop.Excel.Workbook, _
    ByVal SaveAsUI As Boolean, _
    ByRef Cancel As Boolean) Handles Application.WorkbookBeforeSave

    If Globals.Factory.HasVstoObject(Wb) = True Then
        For Each interopSheet As Excel.Worksheet In Wb.Worksheets
            If Globals.Factory.HasVstoObject(interopSheet) = True Then
                Dim vstoSheet As Worksheet = Globals.Factory.GetVstoObject(interopSheet)
                If vstoSheet.Controls.Count > 0 Then
                    System.Windows.Forms.MessageBox.Show( _
                        "The VSTO controls are not persisted when you" _
                        + " save and close this workbook.", _
                        "Controls Persistence", _
                        System.Windows.Forms.MessageBoxButtons.OK, _
                        System.Windows.Forms.MessageBoxIcon.Warning)
                    Exit For
                End If
            End If
        Next
    End If
End Sub
void Application_WorkbookBeforeSave(
    Microsoft.Office.Interop.Excel.Workbook Wb, bool SaveAsUI, 
    ref bool Cancel)
{            
    if (Globals.Factory.HasVstoObject(Wb) == true)
    {                
        foreach (Excel.Worksheet interopSheet in Wb.Worksheets)
        {
            if (Globals.Factory.HasVstoObject(interopSheet) ==  true)
            {
                Worksheet vstoSheet = Globals.Factory.GetVstoObject(interopSheet);
                if (vstoSheet.Controls.Count > 0)
                {
                    System.Windows.Forms.MessageBox.Show(
                        "The VSTO controls are not persisted when you"
                        + " save and close this workbook.",
                        "Controls Persistence",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Warning);
                    break;
                }
            }
        }
    }
}

Sicurezza di .NET Framework

Vedere anche

Riferimenti

ApplicationFactory Interfaccia

Overload GetVstoObject

Spazio dei nomi Microsoft.Office.Tools.Excel

Altre risorse

Estensione in fase di esecuzione di documenti di Word e di cartelle di lavoro di Excel in componenti aggiuntivi a livello di applicazione

Getting Extended Objects from Native Office Objects in Document-Level Customizations