WorkbookExtensions.GetVstoObject Method (2007 System)

Returns a Microsoft.Office.Tools.Excel.Workbook host item that extends the functionality of the current native workbook object.

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

Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function GetVstoObject ( _
    workbook As _Workbook _
) As Workbook
'Usage
Dim workbook As _Workbook 
Dim returnValue As Workbook 

returnValue = workbook.GetVstoObject()
public static Workbook GetVstoObject(
    this _Workbook workbook
)
[ExtensionAttribute]
public:
static Workbook^ GetVstoObject(
    _Workbook^ workbook
)
public static function GetVstoObject(
    workbook : _Workbook
) : Workbook

Parameters

  • workbook
    Type: _Workbook

    The native workbook object to extend. Do not supply this parameter yourself. When you call this method on an Excel workbook, the runtime supplies this parameter.

Return Value

Type: Microsoft.Office.Tools.Excel.Workbook
A host item that extends the functionality of the current Excel workbook object.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type _Workbook. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

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

Note

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

Limitations in Document-Level Customizations

In a document-level customization, this method has a more limited use than in an application-level add-in. You cannot use it to generate a new extended Microsoft.Office.Tools.Excel.Workbook object for a native workbook object. You can use this method only to get the Microsoft.Office.Tools.Excel.Workbook object that is in the current customization. For more information, see Getting Extended Objects from Native Office Objects in Document-Level Customizations.

If the native workbook is not the underlying object of a Microsoft.Office.Tools.Excel.Workbook object in the current customization, this method returns nulla null reference (Nothing in Visual Basic).

Examples

The following code example creates a Microsoft.Office.Tools.Excel.Workbook host item for the active Excel workbook. Next, the example checks whether there are any smart tags defined for this workbook. The code displays either the smart tags or a message that none were found. To use this code, run it from the ThisAddIn class in an add-in project for Excel.

Dim vstoWorkbook As Workbook = _
    Me.Application.ActiveWorkbook.GetVstoObject()

Dim sb As StringBuilder = New StringBuilder()
If vstoWorkbook.VstoSmartTags.Count > 0 Then
    sb.Append("VSTO smart tags found in this workbook:")
Else
    sb.Append("No VSTO smart tags were found in this workbook.")
End If 

For Each st As SmartTag In vstoWorkbook.VstoSmartTags
    sb.Append(vbCrLf + st.Caption)
Next
System.Windows.Forms.MessageBox.Show(sb.ToString())
Workbook vstoWorkbook = 
    this.Application.ActiveWorkbook.GetVstoObject();

StringBuilder sb = new StringBuilder();
if (vstoWorkbook.VstoSmartTags.Count > 0)
{
    sb.Append("VSTO smart tags found in this workbook:");
}
else
{
    sb.Append("No VSTO smart tags were found in this workbook.");
}

foreach (SmartTag st in vstoWorkbook.VstoSmartTags)
{
    sb.Append("\r\n" + st.Caption);
}
System.Windows.Forms.MessageBox.Show(sb.ToString());

.NET Framework Security

See Also

Reference

WorkbookExtensions Class

WorkbookExtensions Members

Microsoft.Office.Tools.Excel.Extensions Namespace

Other Resources

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

Getting Extended Objects from Native Office Objects in Document-Level Customizations

Extension Methods (C# Programming Guide)

Extension Methods (Visual Basic)

Change History

Date

History

Reason

July 2008

New method.

SP1 feature change.