How to get selected cells from an office ribbon control
c#, excel-2010, vsto
Solution
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:
- ApplicationClass.Selection Property
- Application.Selection Property
Problem
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.