Udostępnij za pośrednictwem


ApplicationFactory.HasVstoObject Method

Definition

Overloads

HasVstoObject(_Workbook)

Returns a value that indicates whether a Workbook host item exists for the specified Excel workbook object.

HasVstoObject(_Worksheet)

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

HasVstoObject(ListObject)

Returns a value that indicates whether a ListObject exists for the specified native list object.

HasVstoObject(_Workbook)

Returns a value that indicates whether a Workbook host item exists for the specified Excel workbook object.

public:
 bool HasVstoObject(Microsoft::Office::Interop::Excel::_Workbook ^ workbook);
public bool HasVstoObject (Microsoft.Office.Interop.Excel._Workbook workbook);
abstract member HasVstoObject : Microsoft.Office.Interop.Excel._Workbook -> bool
Public Function HasVstoObject (workbook As _Workbook) As Boolean

Parameters

workbook
_Workbook

The native workbook object to test. Although this parameter is of type _Workbook, you typically pass a Workbook object to this method.

Returns

true if a Workbook host item exists for the specified Workbook object; otherwise, false.

Examples

The following code example checks whether the current workbook has 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)
                {
                    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;
                }
            }
        }
    }
}
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

Remarks

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 workbook.

Note

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

Applies to

HasVstoObject(_Worksheet)

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

public:
 bool HasVstoObject(Microsoft::Office::Interop::Excel::_Worksheet ^ worksheet);
public bool HasVstoObject (Microsoft.Office.Interop.Excel._Worksheet worksheet);
abstract member HasVstoObject : Microsoft.Office.Interop.Excel._Worksheet -> bool
Public Function HasVstoObject (worksheet As _Worksheet) As Boolean

Parameters

worksheet
_Worksheet

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

Returns

true if a Worksheet host item exists for the specified Worksheet object; otherwise, false.

Examples

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

Remarks

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.

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.

Applies to

HasVstoObject(ListObject)

Returns a value that indicates whether a ListObject exists for the specified native list object.

public:
 bool HasVstoObject(Microsoft::Office::Interop::Excel::ListObject ^ listObject);
public bool HasVstoObject (Microsoft.Office.Interop.Excel.ListObject listObject);
abstract member HasVstoObject : Microsoft.Office.Interop.Excel.ListObject -> bool
Public Function HasVstoObject (listObject As ListObject) As Boolean

Parameters

listObject
ListObject

The native Excel list object to test.

Returns

true if a ListObject exists for the specified ListObject object; otherwise, false.

Examples

The following code example checks each native list object in a worksheet to determine if the list object has 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.

Excel.Worksheet mySheet = (Excel.Worksheet)
    this.Application.Worksheets["Sheet1"];
if (mySheet.ListObjects.Count > 0)
{
    foreach (Excel.ListObject list in mySheet.ListObjects)
    {
        if (Globals.Factory.HasVstoObject(list) == true)
        {
            System.Windows.Forms.MessageBox.Show(
                "The VSTO properties of list objects are not "
                + "persisted when you save and close this workbook.",
                "VSTO ListObject",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Warning);
            break;
        }
    }
}
Dim mySheet As Excel.Worksheet = Me.Application.ActiveSheet
If mySheet.ListObjects.Count > 0 Then
    For Each list As Excel.ListObject In mySheet.ListObjects
        If Globals.Factory.HasVstoObject(list) = True Then
            System.Windows.Forms.MessageBox.Show( _
                "The VSTO properties of list objects are not " _
                + "persisted when you save and close this workbook.", _
                "VSTO ListObject", _
                System.Windows.Forms.MessageBoxButtons.OK, _
                System.Windows.Forms.MessageBoxIcon.Warning)
            Exit For
        End If
    Next
End If

Applies to