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

前端 未结 12 1154
梦谈多话
梦谈多话 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: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.

提交回复
热议问题