Changing the 'Selection'

Nov 11, 2011 at 10:31 AM

Good afternoon,

I am currently getting a selection, lets say for example Sheet1!$C$8:$J$9

What I'm doing with this is splitting it up into two strings, according to the amount of rows, e.g. Sheet1!$C$8:$J$8 and Sheet1!$C$9:$J$9, I am later using selection.innerReference.ToArray, and for my add-in to function how I would like, I need to fudge excel/selection into thinking (or changing the selection) that the user has selected a row, held the CTRL key, and selected another row, rather than interpreting it as a 'single' selection, I'd like selection to think it has different selections depending on how many rows are selected (Im only going to cater for two row selections anyway).

Is this possible?

Much appreciated.

Nov 11, 2011 at 10:48 AM

I should've been more specific, Im using an ExcelReference called selection, to get the selection. I need to change that in an IF statement to a different selection depending on the outcome, this is what I'm trying to do

Apologies.

Nov 11, 2011 at 1:35 PM

Seems I solved this using a hacked together function, can convert my string to a range, then run that function passing in my Range and return an ExcelReference which I can assign 'selection' to, sorted!

public static ExcelReference ToXlRef(this Excel.Range o) 
{ 
    // First area 
    ExcelReference xlref = new ExcelReference(o.Row - 1, o.Row - 1 + 
    o.Rows.Count - 1, o.Column - 1, o.Column - 1 + o.Columns.Count - 1, 
    o.Worksheet.Name); 
    // Additional areas 
    if (o.Areas.Count > 1) 
    { 
        for (int i = 2; i <= o.Areas.Count; i++) 
        { 
            xlref.AddReference(o.Areas[i].Row - 1, o.Areas[i].Row - 1 
            + o.Areas[i].Rows.Count - 1, o.Areas[i].Column - 1, o.Areas[i].Column 
            - 1 + o.Areas[i].Columns.Count - 1); 
        } 
    } 
    return xlref; 
}