Compartilhar via


ControlCollection.AddListObject Method

Definition

Overloads

AddListObject(ListObject)

Adds a Visual Studio ListObject to the control collection based on the specified native ListObject.

AddListObject(Range, String)

Adds a new ListObject control to the worksheet at the range specified.

AddListObject(ListObject)

Adds a Visual Studio ListObject to the control collection based on the specified native ListObject.

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

Parameters

listObject
ListObject

The native ListObject.

Returns

The Visual Studio ListObject that was added to the control collection.

Exceptions

The listObject argument is null, or has zero length.

A ListObject control with the same name has already been added to the ControlCollection instance.

Remarks

You should not call this method directly from your code. Instead, call the Microsoft.Office.Tools.Excel.Extensions.ListObjectExtensions.GetVstoObject extension method to obtain a Visual Studio Microsoft.Office.Tools.Excel.ListObject.

The Microsoft.Office.Tools.Excel.Extensions.ListObjectExtensions.GetVstoObject extension method uses this method to generate a Visual Studio Microsoft.Office.Tools.Excel.ListObject that corresponds to an instance of a native Microsoft.Office.Interop.Excel.ListObject.

The Microsoft.Office.Tools.Excel.Extensions.ListObjectExtensions.GetVstoObject extension method creates an extended Microsoft.Office.Tools.Excel.ListObject only once. It returns the same instance of the existing extended object in subsequent calls. In contrast, AddListObject creates a new extended Microsoft.Office.Tools.Excel.ListObject each time.

Applies to

AddListObject(Range, String)

Adds a new ListObject control to the worksheet at the range specified.

public:
 Microsoft::Office::Tools::Excel::ListObject ^ AddListObject(Microsoft::Office::Interop::Excel::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Excel.ListObject AddListObject (Microsoft.Office.Interop.Excel.Range range, string name);
abstract member AddListObject : Microsoft.Office.Interop.Excel.Range * string -> Microsoft.Office.Tools.Excel.ListObject
Public Function AddListObject (range As Range, name As String) As ListObject

Parameters

range
Range

A Range that provides the bounds for the control.

name
String

The name of the control that can be used to index the control in the ControlCollection instance.

Returns

The ListObject control that was added to the ControlCollection instance.

Exceptions

The name or range argument is null, or the name argument has zero length.

A control with the same name is already in the ControlCollection instance.

The range that was specified is not valid. Multi-area ranges cannot be used. The range should be on the same worksheet as the ControlCollection instance.

Examples

The following code example adds a ListObject control to the top of the worksheet, and then displays a message box showing the number of rows in the ListObject.

private void ExcelAddListObject()
{
    Microsoft.Office.Tools.Excel.ListObject list1 =
        this.Controls.AddListObject(this.Range["A1", "C5"], 
        "list1");
    MessageBox.Show("The list object has " + 
        list1.Range.Rows.Count.ToString() + " rows.");
}
Private Sub ExcelAddListObject()
    Dim List1 As Microsoft.Office.Tools.Excel. _
        ListObject = Me.Controls.AddListObject( _
        Me.Range("A1", "C5"), "List1")

    MessageBox.Show("The list object has " _
        & List1.Range.Rows.Count.ToString() + " rows.")

End Sub

Remarks

The AddListObject method enables you to add ListObject controls to the end of the ControlCollection. To remove a ListObject control that was previously added programmatically, use the Remove method.

Applies to