This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge
public Microsoft.Office.Tools.Excel.Workbook GetVstoObject (Microsoft.Office.Interop.Excel._Workbook workbook);

Parameters

The native workbook object for which to retrieve the extended object. Although this parameter is of type _Workbook , you typically pass a Workbook object to this method.

Returns

Examples

The following code example creates a Microsoft.Office.Tools.Excel.Workbook host item for the active Excel workbook. 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.

Workbook vstoWorkbook =
    Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook);
foreach (SmartTag st in vstoWorkbook.VstoSmartTags)
    System.Windows.Forms.MessageBox.Show(st.Caption);
Dim vstoWorkbook As Workbook = _
    Globals.Factory.GetVstoObject(Me.Application.ActiveWorkbook)
For Each st As SmartTag In vstoWorkbook.VstoSmartTags
    System.Windows.Forms.MessageBox.Show(st.Caption)
    	

Remarks

Call this method in an application-level add-in to customize any workbook that is open in Excel. This method generates a new Microsoft.Office.Tools.Excel.Workbook 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.Workbook object. For more information, see Extending Word Documents and Excel Workbooks in VSTO Add-ins at Run Time.

VSTO follows the “Automation Security” Office group policy setting when it is set to “Macros enabled” or “Disable Macros by default”. However, VSTO will always allow automation to run if the setting is set to “Use application macro security level”, regardless of the application setting. To prevent VSTO from allowing automation to run, choose the “Disable Macros by default” setting.

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.

public Microsoft.Office.Tools.Excel.Worksheet GetVstoObject (Microsoft.Office.Interop.Excel._Worksheet worksheet);

Parameters

The native worksheet object for which to retrieve the extended object. Although this parameter is of type _Worksheet, you typically pass a Worksheet object to this method.

Returns

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 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
    End If
End Sub
    	

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 VSTO Add-ins at Run Time.

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.

public Microsoft.Office.Tools.Excel.ListObject GetVstoObject (Microsoft.Office.Interop.Excel.ListObject listObject);

Parameters

Examples

The following code example creates a Microsoft.Office.Tools.Excel.ListObject 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)
    ListObject vstoListObject =
        Globals.Factory.GetVstoObject(mySheet.ListObjects[1]);
    vstoListObject.SetDataBinding(ds, "Product", "Name");
Dim mySheet As Excel.Worksheet = Me.Application.ActiveSheet
If mySheet.ListObjects.Count > 0 Then
    Dim vstoListObject As ListObject = _
        Globals.Factory.GetVstoObject(mySheet.ListObjects(1))
    vstoListObject.SetDataBinding(ds, "Product", "Name")
End If
    	

Remarks

Call this method in an application-level add-in to customize any list object in an Excel worksheet. This method generates a new Microsoft.Office.Tools.Excel.ListObject 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.ListObject object. For more information, see Extending Word Documents and Excel Workbooks in VSTO Add-ins at Run Time.