Worksheet.GetVstoObject Method (2007 System)

Returns an Excel worksheet host item that extends the functionality of the specified native 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 GetVstoObject ( _
    worksheet As _Worksheet _
) As Worksheet
'Usage
Dim worksheet As _Worksheet 
Dim returnValue As Worksheet 

returnValue = Worksheet.GetVstoObject(worksheet)
public static Worksheet GetVstoObject(
    _Worksheet worksheet
)
public:
static Worksheet^ GetVstoObject(
    _Worksheet^ worksheet
)
public static function GetVstoObject(
    worksheet : _Worksheet
) : Worksheet

Parameters

  • worksheet
    Type: _Worksheet

    A native Excel worksheet object.

Return Value

Type: Microsoft.Office.Tools.Excel.Worksheet
A Visual Studio Tools for Office Excel worksheet host item.

Remarks

Calling this method is equivalent to calling the WorksheetExtensions.GetVstoObject 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.GetVstoObject method should be called on a Worksheet object.

Call this method in an application-level add-in to customize a specific worksheet of any workbook that is open in Excel. This method returns a Microsoft.Office.Tools.Excel.Worksheet host item.

This method generates a new extended 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.

Note

The worksheet parameter is of type _Worksheet, which is the parent interface of Worksheet. Therefore, this method extends both types: _Worksheet and Worksheet. Typically, when you reference an Excel worksheet, you use a Worksheet.

Limitations in Document-Level Customizations

In a document-level customization project, this method has a more limited use than in an application-level add-in. You cannot use it to generate a new Visual Studio Tools for Office Excel worksheet for a native Excel worksheet. You can use this method to get an existing Visual Studio Tools for Office Excel worksheet that was part of the customization project at design time. For more information, see Getting Extended Objects from Native Office Objects in Document-Level Customizations.

If the native Excel worksheet is not the underlying object of a Visual Studio Tools for Office Excel worksheet, this method returns nulla null reference (Nothing in Visual Basic).

Examples

The following code example gets the Visual Studio Tools for Office worksheet object for Sheet2 by calling the GetVstoObject method on the Visual Studio Tools for Office worksheet type. The example then adds a ListObject host control to the worksheet. To use this code, run it from the ThisAddIn class in an add-in project for Excel.

Private Sub AddVstoHostControlToWorksheet()
    Dim interopWorksheet As Excel.Worksheet = _
        CType(Me.Application.ActiveWorkbook.Worksheets("Sheet2"),  _
        Excel.Worksheet)

    Dim vstoWorksheet As Worksheet = Worksheet.GetVstoObject( _
        interopWorksheet)

    Dim myTable As ListObject = vstoWorksheet.Controls.AddListObject( _
        vstoWorksheet.Range("A1"), "myTable")
    myTable.InsertRowRange.Value2 = "10" 
End Sub
private void AddVstoHostControlToWorksheet()
{
    Excel.Worksheet interopWorksheet = (Excel.Worksheet)
        this.Application.ActiveWorkbook.Worksheets["Sheet2"];
    Worksheet vstoWorksheet = Worksheet.GetVstoObject(
        interopWorksheet);

    ListObject myTable = vstoWorksheet.Controls.AddListObject(
        vstoWorksheet.Range["A1", missing],
        "myTable");
    myTable.InsertRowRange.Value2 = "10";
}

.NET Framework Security

See Also

Reference

Worksheet Class

Worksheet Members

Microsoft.Office.Tools.Excel Namespace

GetVstoObject

Other Resources

Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time

Host Items and Host Controls Overview

Extension Methods (C# Programming Guide)

Extension Methods (Visual Basic)

Change History

Date

History

Reason

July 2008

New topic.

SP1 feature change.