Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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's visibility was true. But I need to hide the excel at the time of execution. So I changed the visibility to false. Like,

  _destExcelApp = new Excel.ApplicationClass();
  _destExcelApp.Visible = false;

Now its showing an exception like

Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))

How to solve this one?

like image 469
Manoj Nayak Avatar asked Dec 11 '13 08:12

Manoj Nayak


4 Answers

I ran into this same error being thrown when I deployed my application onto a machine without a fully activated and licensed installation of Excel. I spent hours trying to diagnose the issue. Make sure you check your Office installations to make sure they are complete.

like image 63
Crash5998 Avatar answered Nov 16 '22 05:11

Crash5998


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.

like image 23
Joost Avatar answered Nov 16 '22 04:11

Joost


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

MS Word Set default program prompt

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

like image 6
CJBS Avatar answered Nov 16 '22 05:11

CJBS


I was facing the same error and many solutions suggested were not working for me. I had an application running in windows 8 and I found that the problem was Excel always asking to choose default application for "xlsx" extensions. When executing the application no window dialog appeared, just the error was shown.

I solved the problem going to Control Panel > Programs > Default Programs and setting Microsoft Office Excel 2016 as default program for xlsx files.

like image 5
Alielson Piffer Avatar answered Nov 16 '22 04:11

Alielson Piffer