The Names collection represents a set of ranges in the workbook that have been given names so that the range can be accessed by a name in a formula or by your code accessing the Names collection. The user can create and edit names using the Name Box, as shown in Figure 5-2, or by using the Name menu in the Insert menu. Also, names are sometimes automatically created by features of Excel. For example, when the user defines a custom print area, Excel creates a named range with the name Print_Area.
Iterating over the Names Collection
The Names collection has a GetEnumerator method that allows it to be iterated over using the foreach keyword in C#. For example, the following snippet iterates the Names collection associated with a workbook and displays the name of each Name object as well as the address of the range it refers to in standard format (for instance, "=Sheet1!$A$5").
foreach (Excel.Name name in workbook.Names) { Console.WriteLine(String.Format( "{0} refers to {1}", name.Name, name.RefersTo)); }
Accessing a Name in the Names Collection
To access a Name in the Names collection, you use a method called Item, which takes three optional parameters, as shown in Table 5-13.
Parameter Name |
Type |
What It Does |
---|---|---|
Index |
object |
Pass the name of the Name or the index of the Name in the Names collection. |
IndexLocal |
object |
Pass the localized name of the Name. A localized name typically exists when an Excel feature has created the name. |
RefersTo |
object |
Pass the standard format refers to address (=Sheet1!$A$5) to get back the Name object that refers to that address. |
Listing 5-22 shows some code that creates a Name and then accesses it in several ways. It creates the Name using the Add method that takes the name to be used for the Name object and the standard format address string (such as "=Sheet1!$A$5") that the newly created name will refer to.
Listing 5-22. A VSTO Customization That Creates a Name Object and Accesses It
private void ThisWorkbook_Startup(object sender, EventArgs e) { Excel.Names names = this.Names; names.Add("MyName", "=Sheet1!$A$5", missing, missing, missing, missing, missing, missing, missing, missing, missing); Excel.Name name1 = names.Item(missing, missing, "=Sheet1!$A$5"); MessageBox.Show(String.Format( "Name: {0} RefersTo: {1} RefersToR1C1: {2} Count: {3}", name1.Name, name1.RefersTo, name1.RefersToR1C1, name1.RefersToRange.Cells.Count)); Excel.Name name2 = names.Item("MyName", missing, missing); MessageBox.Show(String.Format( "Name: {0} RefersTo: {1} RefersToR1C1: {2} Count: {3}", name2.Name, name2.RefersTo, name2.RefersToR1C1, name2.RefersToRange.Cells.Count)); }
The Name Object
Given a Name object, you will commonly use several properties. The Name returns the name as a string. The RefersTo property returns the standard format address as a string that the Name refers to. The RefersToR1C1 returns the "rows and columns" format address as a string (such as "=Sheet1!R26C9") that the Name refers to. Most importantly, the RefersToRange property returns an Excel Range object representing the range of cells that the name was assigned to.
To hide the name from the Define Name dialog and the Name Box drop-down, you can set the Visible property to false. To delete a Name, use the Delete method.
Part One. An Introduction to VSTO
An Introduction to Office Programming
Introduction to Office Solutions
Part Two. Office Programming in .NET
Programming Excel
Working with Excel Events
Working with Excel Objects
Programming Word
Working with Word Events
Working with Word Objects
Programming Outlook
Working with Outlook Events
Working with Outlook Objects
Introduction to InfoPath
Part Three. Office Programming in VSTO
The VSTO Programming Model
Using Windows Forms in VSTO
Working with Actions Pane
Working with Smart Tags in VSTO
VSTO Data Programming
Server Data Scenarios
.NET Code Security
Deployment
Part Four. Advanced Office Programming
Working with XML in Excel
Working with XML in Word
Developing COM Add-Ins for Word and Excel
Creating Outlook Add-Ins with VSTO