Opening Excel - Call rejected by Callee

1

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
excel
powershell
asked on Stack Overflow Jan 5, 2016 by Will JM • edited Jan 5, 2016 by Ansgar Wiechers

2 Answers

1

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
answered on Stack Overflow Jun 16, 2016 by Jthorpe • edited Jun 16, 2016 by Jthorpe
0

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.

answered on Stack Overflow Apr 7, 2020 by Zezombye

User contributions licensed under CC BY-SA 3.0