Here's what I am trying to achieve with a Excel Addin. Excel user selects a range and he deletes it. I want to write cell data (whatever he deletes) in that range to a log file. Multiple questions on SO say that you can't get deleted range in SheetChange event handler, you need to store it. I am storing range in a global variable LastRange
in selection change event method handler. I need LastRange
in SheetChange event handler.
The problem is I get COM exception --
COM object that has been separated from its underlying RCW cannot be used.
I know somewhere that LastRange
is getting freed and hence the exception.
I referred to another SO question here. Problem persists even though I have strong references to the delegate.
private ADXExcelSheet_EventHandler sheetChangeEventHandler;
private ADXExcelSheet_EventHandler sheetSelectionChangeEventHandler;
private void InitializeComponent()
{
sheetChangeEventHandler = new AddinExpress.MSO.ADXExcelSheet_EventHandler(SheetChange);
excelEvents.SheetChange += sheetChangeEventHandler;
sheetSelectionChangeEventHandler = new ADXExcelSheet_EventHandler(SheetSelectionChange);
excelEvents.SheetSelectionChange += sheetSelectionChangeEventHandler;
}
//....
Range LastRange;
public void SheetSelectionChange(object sender, object sheet, object range)
{
LastRange = (range as Range);
}
public void SheetChange(object sender, object sheet, object range)
{
ClassX.Method1(range as Range, LastRange);
}
Not sure why LastRange
is still null inside Method1. LastRange
is correctly passed from SheetChange to Method1()
. Any other ideas to get this working?