NamedRange.Selected Event

Occurs when the NamedRange control is selected.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)

event DocEvents_SelectionChangeEventHandler Selected

This event is raised when the selection changes to the NamedRange control.

The following code example demonstrates a handler for the Selected event that displays the address of a cell that is selected in the NamedRange.

This version is for a document-level customization.


Microsoft.Office.Tools.Excel.NamedRange selectedEventRange;
private void DisplaySelectedRange()
{
    selectedEventRange = this.Controls.AddNamedRange(
        this.Range["B1", "E5"], "selectedEventRange");
    this.selectedEventRange.Selected += new 
        Microsoft.Office.Interop.Excel.
        DocEvents_SelectionChangeEventHandler(
        selectedEventRange_Selected);
}

void selectedEventRange_Selected(
    Microsoft.Office.Interop.Excel.Range Target)
{
    string selectedRange = Target.get_Address(missing, missing,
        Excel.XlReferenceStyle.xlA1, missing, missing);

    MessageBox.Show(this.Name + ": " + selectedRange + 
        " was selected.");
}


This version is for an application-level add-in.


NamedRange selectedEventRange;
private void DisplaySelectedRange()
{
    Worksheet vstoWorksheet =
        Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.Worksheets[1]);
    selectedEventRange = vstoWorksheet.Controls.AddNamedRange(
        vstoWorksheet.Range["B1", "E5"], "selectedEventRange");
    selectedEventRange.Selected += new
        Excel.DocEvents_SelectionChangeEventHandler(
        selectedEventRange_Selected);
}

void selectedEventRange_Selected(
    Microsoft.Office.Interop.Excel.Range Target)
{
    string selectedRange = Target.get_Address(missing, missing,
        Excel.XlReferenceStyle.xlA1, missing, missing);

    Excel.Worksheet nativeWorksheet = (Excel.Worksheet)
        this.Application.ActiveWorkbook.Worksheets[1];
    System.Windows.Forms.MessageBox.Show(
        nativeWorksheet.Name + ": " + selectedRange +
        " was selected.");
}


Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft