how to solve Exception:Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)) in C#?

前端 未结 12 1140
梦谈多话
梦谈多话 2020-11-30 13:43

I have written a C# code in console application to open two excels and copy and paste data from one excel to another excel. It was working fine until the destination excel\'

相关标签:
12条回答
  • 2020-11-30 14:21

    Ran into this problem on my machine. Excel is fully activated and already the default program for .xlsx files. I was loading a workbook template I created with pivot tables and having a script update the data in the tables. Turns out that if the pivot tables are set to "Refresh data when opening the file" under Pivot Table Options > Data, it causes some sort of threading contention issues.
    Disabling the refresh on opening solved the issue.

    0 讨论(0)
  • 2020-11-30 14:23

    Ensure that MS Word/Excel is not showing a dialog box that needs a response.

    I set a breakpoint on the line that caused the failure, then set .Visible to true in PowerShell, to find this:

    $word.Visible = $true
    

    After I clicked 'Yes' and updated the settings, after I re-ran my scripted COM interactions, they succeeded.

    0 讨论(0)
  • 2020-11-30 14:24

    I solved this behaviour with the help of this question:

    Strange behaviour of "Call was rejected by callee." exception with Excel

    The issue was simply that the Workbook.Open hadn't finished when I gave a Worksheet.SaveAs command. So sometimes, the script would work, sometimes not.

    I simply added a pause in the script after Workbook.Open and it worked. I went on to find a property Ready, which allowed me to do exactly what I wanted:

        $excel = New-Object -ComObject "Excel.Application" -ea Stop
        $wb = $excel.Workbooks.Open($workbook)
        $sheet = $wb.Sheets("List")
        while (-not $excel.Ready) {
            sleep 1
        }
        $sheet.SaveAs($csvpath,6)
    

    So in my case, it had nothing to do with non-activated or corrupted Excel installations.

    0 讨论(0)
  • 2020-11-30 14:28

    Are you copying a range of information from one document to another, or are you going back and forth between the 2 documents copying cell by cell? Excel is single threaded, so if you go back and forth, it could cause this issue.

    0 讨论(0)
  • 2020-11-30 14:29

    In my case, I simply restarted my machine and found out that there was a windows update pending. Restarting the machine solved my problem.

    0 讨论(0)
  • 2020-11-30 14:30

    I encountered this Error today in Excel 2016.

    We found out that the computer with this problem had some add-ins activated.

    Strangely that one pc took ages to start excel. after deactivating the add-ins our program worked fine.

    Strangely we couldn´t reproduce this on our dev pc´s.

    0 讨论(0)
提交回复
热议问题