Excel VSTO: Selecting range according to properties
I found the following solution:
// Gets the selected range
Excel.Worksheet activeWorksheet = Globals.ThisAddIn.Application.ActiveSheet;
Excel.Range activeRange = Globals.ThisAddIn.Application.Selection.Cells;
Excel.Range expandedRange = activeRange.EntireRow; // selects entire rows
List<string laddr = new List<string();
foreach (Excel.Range r in expandedRange.Rows) // this returns only the rows not hidden
if (!r.Hidden)
laddr.Add(r.Address[true,true]);
Excel.Range selectedRange = activeWorksheet.Range[string.Join(";", laddr)];
selectedRange.Select();
So I get the string representing the range in each row and then use Range[] to obtain the desired range object.
|