Worksheet.HasVstoObject Method

Returns a value that indicates whether an Excel Microsoft.Office.Tools.Excel.Worksheet host item has been created for the specified native Excel Microsoft.Office.Interop.Excel._Worksheet.

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

Syntax

'Declaration
Public Shared Function HasVstoObject ( _
    worksheet As _Worksheet _
) As Boolean
'Usage
Dim worksheet As _Worksheet
Dim returnValue As Boolean

returnValue = Worksheet.HasVstoObject(worksheet)
public static bool HasVstoObject(
    _Worksheet worksheet
)

Parameters

Return Value

Type: System.Boolean
true if a worksheet host item has been created for the specified native Excel worksheet; otherwise, false.

Remarks

Calling this method is equivalent to calling the WorksheetExtensions.HasVstoObject method. The only difference is that this is a static method (Shared in Visual Basic) that should be called on the Microsoft.Office.Tools.Excel.Worksheet type, whereas the WorksheetExtensions.HasVstoObject method should be called on a Microsoft.Office.Interop.Excel.Worksheet object.

For example, you can call this method in an application-level add-in. If it returns true, you can test for the existence of managed controls that you want to persist before closing or saving the Excel workbook.

You can also use this method in a document-level customization.

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 extends 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 checks whether the current workbook has an extended Visual Studio Tools for Office object. If so, it iterates through the workbook's worksheets and checks each one for the existence of a corresponding Visual Studio Tools for Office worksheet object. If a Visual Studio Tools for Office worksheet object exists for a specific worksheet, the example gets this object and displays a message to the user, and then exits. This code example uses the event handler of the WorkbookBeforeClose() event to perform the check. To use this code, run it from the ThisAddIn class in an add-in project for Excel.

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

    If Workbook.HasVstoObject(Wb) = True Then
        For Each interopSheet As Excel.Worksheet In Wb.Worksheets
            If Worksheet.HasVstoObject(interopSheet) = True Then
                Dim vstoSheet As Worksheet = Worksheet.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_WorkbookBeforeClose(
    Microsoft.Office.Interop.Excel.Workbook Wb, ref bool Cancel)
{
    if (Workbook.HasVstoObject(Wb) == true)
    {
        foreach (Excel.Worksheet interopSheet in Wb.Worksheets)
        {
            if (Worksheet.HasVstoObject(interopSheet) == true)
            {
                Worksheet vstoSheet = Worksheet.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

Worksheet Class

Worksheet Members

Microsoft.Office.Tools.Excel Namespace

WorksheetExtensions.HasVstoObject

Other Resources

Extension Methods (C# Programming Guide)

Extension Methods (Visual Basic)