I am trying to open Excel with Com addins via PowerShell and keep getting a "Call was rejected by callee" error, please see code below. Any thoughts?
$a = New-Object -ComObject Excel.Application
$a.Visible = $True
Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)) At line:2 char:1 + $a.Visible = $True + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
This reference is more appropriate for PowerShell users. The code attaches message filters to the current thread, which incidentally means that powershell has to be run in STA mode (the default for powershell 3.0+).
For some messaging and a small pause between re-calls to excel, I added this line to the list of using
declarations:
using System;
using System.Runtime.InteropServices;
using System.Threading; // <- added this line
and I added these lines to the re-try handler:
if (dwRejectType == 2)
// flag = SERVERCALL_RETRYLATER.
{
// Retry the thread call immediately if return >=0 &
// <100.
string wait_message = "Wait for Excel RPC channel"; // <- added this line
Console.WriteLine(wait_message); // <- added this line
Thread.Sleep(100); // <- added this line
return 99;
}
In addition, if a user happens to hit the keyboard or click on a visible application, Excel will throw RPC_E_CALL_REJECTED
continuously, unless you call this before your set your application instance to visible.
$Application.Interactive = $false
After sourcing the referenced script, your code becomes:
$a = New-Object -ComObject Excel.Application
AddMessageFilterClass
$Application.Interactive = $false
$a.Visible = $True
FYI this also seems to occur when Visible
is $false
, for example:
New-Object -ComObject Excel.Application -Property @{Visible = $false}
Also, in my case it only threw the error when launched via WinRM; if I executed the script on the host machine, it worked perfectly fine.
User contributions licensed under CC BY-SA 3.0