ApplicationFactory.HasVstoObject Method (_Worksheet)


Returns a value that indicates whether a Microsoft.Office.Tools.Excel.Worksheet host item exists for the specified Excel worksheet object.

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

bool HasVstoObject(
	_Worksheet worksheet


Type: Microsoft.Office.Interop.Excel._Worksheet

The native worksheet object to test. Although this parameter is of type Microsoft.Office.Interop.Excel._Worksheet, you typically pass a Microsoft.Office.Interop.Excel.Worksheet object to this method.

Return Value

Type: System.Boolean

true if a Microsoft.Office.Tools.Excel.Worksheet host item exists for the specified Microsoft.Office.Interop.Excel.Worksheet object; otherwise, false.

You can call this method in an application-level add-in to test for the existence of managed controls that you want to persist before closing or saving the Excel worksheet.


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.

The following code example checks whether worksheets in the current workbook have an associated host item. To use this code, run it from the ThisAddIn class in an Excel add-in project that targets the .NET Framework 4 or the .NET Framework 4.5.

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)
                        "The VSTO controls are not persisted when you"
                        + " save and close this workbook.",
                        "Controls Persistence",
Return to top