Joe Johnston
Joe Johnston

Reputation: 2936

How to get selected cells from an office ribbon control

I want to get the active range from a ribbon control. There should be a way to access the selected cells ONLY when a control on the ribbon needs it.

Currently this is how I am doing it;

public partial class ThisAddin
{
    private void SheetSelectionChange(object sh, Range target)
    {
        int count = target.Count;

            if (count < 5000) // This is for performance reasons 
            {
                //Set a custom range property in the office ribbon
                Req_Tool.ActiveRange = target; 
            {
    }
}

I feel this is weak and wasteful.
Firstly, EVERY time a selection changes my code is run. Secondly, I have two copies of the selection wether its used or not.

There has to be a better way to do this that I am overlooking.

Upvotes: 3

Views: 2411

Answers (1)

Peter Majeed
Peter Majeed

Reputation: 5352

You can access the currently selected range in the active sheet by accessing the Selection property of the Application object.

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    this.ActiveRange = (Excel.Range)Globals.ThisAddIn.Application.Selection;
}

MSDN documentation:

Upvotes: 6

Related Questions