none
Issue with worksheet.Select(true) after hosting Excel window in WPF application RRS feed

  • Question

  • The issue is with Office 2013.

    We are hosting excel workbook window in WPF application using HwndHost class. In overridden BuildWindowCore method of HwndHost class, we are creating a MDICLIENT Window and setting this MDICLIENT window handle as parent for Excel main window handle and returning HandleRef object of MDICLIENT window handle from BuildWindowCore method.

    Once HwndHost control is loaded, we are selecting sheet of excel in the loaded event of HwndHost control. The code for selection of sheet is given below.

    dynamic workSheet = this.excelApplication.Workbooks[1].Worksheets[sheetName];

    this.excelApplication.Workbooks[1].Activate()

    workSheet.Activate();

    workSheet.Select(true);

     

    The first time execution of workSheet.Select(true) halt for few seconds and then it throws exception “System.Runtime.InteropServices.ComException” with message “The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))”. But it executes successfully without delay on subsequent calls. Sometimes the execution halt for minutes on different Systems.

    This absurd behaviour is seen only once in application instance life time and that too after excel window gets loaded in WPF application. If we execute workSheet.Select(true) before loading of window then it executes properly. Once workSheet.Select(true) executed for first time with exception, it executes successfully without delay on subsequent calls.


    Tuesday, March 31, 2015 6:10 AM

All replies

  • This could mean the COM object crashed. Had this happen with a macro in Excel that crashed Exel,do you have any macro in this sheet ?
    Thursday, April 2, 2015 5:41 AM
  • No, I dont have any macro in the sheet and it is happpening with all workbooks. The problem is consistent with all workbooks and also it throws exception only for first time in the application. If I execute it again in same application instance, it works properly.
    Thursday, April 2, 2015 6:12 AM