Issue in order of event handler execution between office 2016 and office 365 Version.

sathish viswanathan 6 Reputation points
2021-08-24T16:44:45.44+00:00

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();  
  }



  static void ExcelEventHandler_SheetCalculate(object sheet)
    {
        MessageBox.Show(String.Format("SheetCalculate"));
    }
    private 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"));
    }

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

Why the order of events not same in both office versions? Any help or insight to this would be greatly appreciated.

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,075 questions
Office Management
Office Management
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Management: The act or process of organizing, handling, directing or controlling something.
2,208 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,739 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. sathish viswanathan 6 Reputation points
    2021-08-30T06:09:35.503+00:00

    Can I have any update on this ticket?

    0 comments No comments

  2. sathish viswanathan 6 Reputation points
    2021-09-03T12:12:35.233+00:00

    Could you suggest us? How to rearrange paste event in Office 365?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.