I’m working on an import (from Excel) dialog to select ranges of cells.
When the range is selected, I use the event sink to catch the event and highlight the first row and first column.
I need to unhighlight the previous selection’s first row and column. I don’t think it’s safe to just get the selected range at the time the selection changes and remember it, such as (pseudocode for brevity and clarity):
OnSelectionChange() { if (m_PrevSelection) UnHilite(m_PrevSelection); HiliteCurrentSelection(); GetSelectedRange(m_PrevSelection); }
I’m guessing that just holding onto that range (obtained from _Application::Selection) without releasing it is going to cause all sorts of problems. I haven’t found a way to copy the range (IRange Copy just copies cell contents from one range to another).
I guess I could take the range’s cell addresses and store those, then recreate a range from them when I need to do the unhighlighting. This would seem to me to come up often. Is there a more elegant solution?
If you were working in Excel VBA, you could
where Rng is an Excel Range object. I imagine you could replicate this object from where you are.
Or you could store the cell address in a string variable as you suggested, which of course doesn’t require any objects.
Unfortunately, Excel doesn’t keep a history of selections.