We found an issue in order of event handler execution between office 2016 and office 365 Version. We have create sample excel application to reproduce the issue. During paste operation order of event handler different from Office 2016 and office 365.
Please find the code snippet for your reference.
public void PrinttheOrderofExecution()
{
app = new Excel.Application();
app.Visible = true;
workbook = app.Workbooks.Add();
Excel.Worksheet newWorksheet = null;
newWorksheet = (Excel.Worksheet)app.ActiveWorkbook.Sheets.Add();
worksheet = app.ActiveSheet as Worksheet;
// Put a volatile function on the sheet, which will force a recalculation
// of this sheet whenever any calculation takes place.
string cellAddition = "=NOW()";
Range firstCell = worksheet.Range["A1"];
firstCell.Formula = cellAddition;
app.AfterCalculate += new AppEvents_AfterCalculateEventHandler(ExcelEventHandler_AfterCalculate);
_excelEventDisconnects.Add(() => { app.AfterCalculate -= ExcelEventHandler_AfterCalculate; });
app.SheetCalculate += new AppEvents_SheetCalculateEventHandler(ExcelEventHandler_SheetCalculate);
_excelEventDisconnects.Add(() => { app.SheetCalculate -= ExcelEventHandler_SheetCalculate; });
app.SheetChange += new AppEvents_SheetChangeEventHandler(ExcelEventHandler_SheetChange);
_excelEventDisconnects.Add(() => { app.SheetChange -= ExcelEventHandler_SheetChange; });
app.SheetSelectionChange += new AppEvents_SheetSelectionChangeEventHandler(ExcelEventHandler_SheetSelectionChange);
_excelEventDisconnects.Add(() => { app.SheetSelectionChange -= ExcelEventHandler_SheetSelectionChange; });
bool eventFired = true;
string cellValue = "New Value";
Assert.IsNotNull(worksheet);
Range cellToCut = (Range)worksheet.Cells[12, 12];
Range cellToPaste = (Range)worksheet.Cells[14, 14];
cellToCut.Value = cellValue;
cellToCut.Select();
cellToCut.Cut();
cellToPaste.Select();
worksheet.Paste();
Assert.IsTrue(eventFired);
Thread.CurrentThread.Join();
}
void ExcelEventHandler_SheetCalculate(object sheet)
{
MessageBox.Show(String.Format("SheetCalculate"));
}
void ExcelEventHandler_AfterCalculate()
{
MessageBox.Show(String.Format("AfterCalculate"));
}
void ExcelEventHandler_SheetSelectionChange(object Sh, Excel.Range Target)
{
if (app.CutCopyMode == Microsoft.Office.Interop.Excel.XlCutCopyMode.xlCut)
{
MessageBox.Show(String.Format("Sheet Selection Change - Cut Operation"));
}
else if (app.CutCopyMode == Microsoft.Office.Interop.Excel.XlCutCopyMode.xlCopy)
{
MessageBox.Show(String.Format("Sheet Selection Change - Copy Operation"));
}
else
{
MessageBox.Show(String.Format("Sheet Selection Change Operation"));
}
}
void ExcelEventHandler_SheetChange(object Sh, Excel.Range Target)
{
MessageBox.Show(String.Format("SheetChange Operation"));
}
Output
Office 2016 version executes the following order
Sheet Calculate
Sheet Change
Sheet Change
After Calculate
Office 365 version executes the following order
Sheet Change
Sheet Change
Sheet Calculate
After Calculate