Udostępnij za pośrednictwem


ApplicationFactory.HasVstoObject Method (_Workbook)

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

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

Syntax

'Declaration
Function HasVstoObject ( _
    workbook As _Workbook _
) As Boolean
bool HasVstoObject(
    _Workbook workbook
)

Parameters

Return Value

Type: System.Boolean
true if a Microsoft.Office.Tools.Excel.Workbook host item exists for the specified Microsoft.Office.Interop.Excel.Workbook object; otherwise, false.

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.

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.

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

HasVstoObject Overload

Microsoft.Office.Tools.Excel Namespace