Udostępnij za pośrednictwem


ApplicationFactory.GetVstoObject Method (_Worksheet)

Returns a Microsoft.Office.Tools.Excel.Worksheet host item that extends the functionality of the specified native workbook object.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)

Syntax

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

Parameters

Return Value

Type: Microsoft.Office.Tools.Excel.Worksheet
The host item that extends the functionality of the native worksheet object.

Remarks

Call this method in an application-level add-in to customize any worksheet that is open in Excel. This method generates a new Microsoft.Office.Tools.Excel.Worksheet object if no such object has already been generated. Subsequent calls to this method return the cached instance of the existing Microsoft.Office.Tools.Excel.Worksheet object. For more information, see Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time.

Note

The worksheet parameter is of type Microsoft.Office.Interop.Excel._Worksheet, which is the parent interface of Microsoft.Office.Interop.Excel.Worksheet. Therefore, this method can accept objects of both types: Microsoft.Office.Interop.Excel._Worksheet and Microsoft.Office.Interop.Excel.Worksheet. Typically, when you reference an Excel worksheet, you use a Microsoft.Office.Interop.Excel.Worksheet.

Examples

The following code example creates a Microsoft.Office.Tools.Excel.Worksheet host item for each Microsoft.Office.Interop.Excel.Workbook object that has a host item. To use this code, run it from the ThisAddIn class in an Excel add-in project that targets the .NET Framework 4.

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

.NET Framework Security

See Also

Reference

ApplicationFactory Interface

GetVstoObject Overload

Microsoft.Office.Tools.Excel Namespace

Other Resources

Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time

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