Workbook.GetVstoObject Method

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

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 ( _
    workbook As _Workbook _
) As Workbook
'Usage
Dim workbook As _Workbook
Dim returnValue As Workbook

returnValue = Workbook.GetVstoObject(workbook)
public static Workbook GetVstoObject(
    _Workbook workbook
)

Parameters

Return Value

Type: Microsoft.Office.Tools.Excel.Workbook
A Visual Studio Tools for Office Excel workbook host item.

Remarks

Calling this method is equivalent to calling the WorkbookExtensions.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.Workbook type, whereas the WorkbookExtensions.GetVstoObject method should be called on a Microsoft.Office.Interop.Excel.Workbook object.

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

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 Microsoft.Office.Tools.Excel.Workbook object.

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

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 workbook for a native Excel workbook. You can use this method to get an existing Visual Studio Tools for Office Excel workbook 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 workbook is not the underlying object of a Visual Studio Tools for Office Excel workbook, this method returns nulla null reference (Nothing in Visual Basic).

Examples

The following code example calls this method on the Microsoft.Office.Tools.Excel.Workbook type to obtain a Visual Studio Tools for Office Microsoft.Office.Tools.Excel.Workbook object. Next, the example checks whether there are any smart tags defined for this workbook and 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 = Workbook.GetVstoObject( _
    Me.Application.ActiveWorkbook)

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 = Workbook.GetVstoObject(
    this.Application.ActiveWorkbook);

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

Workbook Class

Workbook Members

Microsoft.Office.Tools.Excel Namespace

GetVstoObject

Other Resources

Workbook Host Item

Smart Tags Overview

Extension Methods (C# Programming Guide)

Extension Methods (Visual Basic)