ApplicationFactory.GetVstoObject Method

Definition

Overloads

GetVstoObject(_Workbook)

Returns a Workbook host item that extends the functionality of the specified native workbook object.

GetVstoObject(_Worksheet)

Returns a Worksheet host item that extends the functionality of the specified native workbook object.

GetVstoObject(ListObject)

Returns a ListObject that extends the functionality of the specified native list object.

GetVstoObject(_Workbook)

Returns a Workbook host item that extends the functionality of the specified native workbook object.

public:
 Microsoft::Office::Tools::Excel::Workbook ^ GetVstoObject(Microsoft::Office::Interop::Excel::_Workbook ^ workbook);
public Microsoft.Office.Tools.Excel.Workbook GetVstoObject (Microsoft.Office.Interop.Excel._Workbook workbook);
abstract member GetVstoObject : Microsoft.Office.Interop.Excel._Workbook -> Microsoft.Office.Tools.Excel.Workbook
Public Function GetVstoObject (workbook As _Workbook) As Workbook

Parameters

workbook
_Workbook

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

The host item that extends the functionality of the native workbook object.

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)
Next

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.

Note

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.

Applies to

GetVstoObject(_Worksheet)

Returns a Worksheet host item that extends the functionality of the specified native workbook object.

public:
 Microsoft::Office::Tools::Excel::Worksheet ^ GetVstoObject(Microsoft::Office::Interop::Excel::_Worksheet ^ worksheet);
public Microsoft.Office.Tools.Excel.Worksheet GetVstoObject (Microsoft.Office.Interop.Excel._Worksheet worksheet);
abstract member GetVstoObject : Microsoft.Office.Interop.Excel._Worksheet -> Microsoft.Office.Tools.Excel.Worksheet
Public Function GetVstoObject (worksheet As _Worksheet) As Worksheet

Parameters

worksheet
_Worksheet

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

The host item that extends the functionality of the native worksheet object.

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

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

GetVstoObject(ListObject)

Returns a ListObject that extends the functionality of the specified native list object.

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

Parameters

listObject
ListObject

The native list object for which to retrieve the extended object.

Returns

The object that extends the functionality of the native list object.

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.

Applies to