ControlCollection.AddNamedRange Method (2007 System)
Adds a new NamedRange control to the worksheet at the range specified.
Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel.v9.0 (in Microsoft.Office.Tools.Excel.v9.0.dll)
Syntax
'Declaration
Public Function AddNamedRange ( _
range As Range, _
name As String _
) As NamedRange
'Usage
Dim instance As ControlCollection
Dim range As Range
Dim name As String
Dim returnValue As NamedRange
returnValue = instance.AddNamedRange(range, _
name)
public NamedRange AddNamedRange(
Range range,
string name
)
public:
NamedRange^ AddNamedRange(
Range^ range,
String^ name
)
public function AddNamedRange(
range : Range,
name : String
) : NamedRange
Parameters
range
Type: RangeA Range that provides the bounds for the control.
name
Type: System.StringThe name of the control that can be used to index the control in the ControlCollection instance.
Return Value
Type: Microsoft.Office.Tools.Excel.NamedRange
The NamedRange control that was added to the ControlCollection instance.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | The name or range argument is nulla null reference (Nothing in Visual Basic), or the name argument has zero length. |
ControlNameAlreadyExistsException | A control with the same name is already in the ControlCollection instance. |
InvalidRangeException | 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. |
Remarks
The AddNamedRange method enables you to add NamedRange controls to the end of the ControlCollection. To remove a NamedRange control that was previously added programmatically, use the Remove method.
Examples
The following code example adds a NamedRange control to cell A1, and then sets the value of the named range to 100%.
Private Sub ExcelAddNamedRange()
Dim NamedRange1 As Microsoft.Office.Tools. _
Excel.NamedRange = Me.Controls.AddNamedRange( _
Me.Range("A1"), "NamedRange1")
NamedRange1.Value2 = "100%"
End Sub
private void ExcelAddNamedRange()
{
Microsoft.Office.Tools.Excel.NamedRange namedRange1 =
this.Controls.AddNamedRange(this.Range["A1", missing],
"namedRange1");
namedRange1.Value2 = "100%";
}
.NET Framework Security
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.