共用方式為


WorkbookBase.Names Property

Definition

Gets a Names collection that represents all the names in the workbook (including all worksheet-specific names).

public:
 property Microsoft::Office::Interop::Excel::Names ^ Names { Microsoft::Office::Interop::Excel::Names ^ get(); };
public Microsoft.Office.Interop.Excel.Names Names { get; }
member this.Names : Microsoft.Office.Interop.Excel.Names
Public ReadOnly Property Names As Names

Property Value

A Names collection that represents all the names in the workbook (including all worksheet-specific names).

Examples

The following code example uses the Names property to create three Microsoft.Office.Interop.Excel.Name objects that refer to different ranges on worksheet Sheet1. The example then iterates through every Microsoft.Office.Interop.Excel.Name in the workbook and displays the names in column A of worksheet Sheet1.

This example is for a document-level customization.

private void DisplayWorkbookNames()
{
    this.Names.Add("Name1", Globals.Sheet1.Range["B1", "B5"], true);
    this.Names.Add("Name2", Globals.Sheet1.Range["C1", "C5"], true);
    this.Names.Add("Name3", Globals.Sheet1.Range["D1", "D5"], true);

    for (int i = 1; i <= this.Names.Count; i++)
    {
        Globals.Sheet1.Range["A" + i.ToString()].Value2 =
            this.Names.Item(i);
    }
}
Private Sub DisplayWorkbookNames()
    Me.Names.Add("Name1", Globals.Sheet1.Range("B1", "B5"), _
        True)
    Me.Names.Add("Name2", Globals.Sheet1.Range("C1", "C5"), _
        True)
    Me.Names.Add("Name3", Globals.Sheet1.Range("D1", "D5"), _
        True)

    Dim i As Integer
    For i = 1 To Me.Names.Count
        Globals.Sheet1.Range("A" & i.ToString()).Value2 = _
            Me.Names.Item(i)
    Next i
End Sub

Applies to