我正在尝试通过PowerShell使用Com插件打开Excel并继续获取#34;呼叫被被叫方拒绝"错误,请参阅下面的代码。有什么想法吗?
arr = [{label: "first", data: [1, 2]}, {label: "second", data: [3, 4, 5]}, {label: "third", data: []}, {label: "fourth", data: [6]}]
loop do
arr.each do |hash| # go through each hash
num = hash[:data].delete_at(0) # remove first element in data array
puts "#{hash[:label]}: #{num}" unless num.nil? # output it if it's not nil(means array was empty)
end
break if arr.map { |i| i[:data] }.flatten == [] # stop if all arrays are empty
end
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
答案 0 :(得分:1)
This reference更适合PowerShell用户。代码将消息过滤器附加到当前线程,顺便说一下,powershell必须在STA mode中运行(powershell 3.0 +的默认值)。
对于某些消息传递和重新调用excel之间的小暂停,我将此行添加到using
声明列表中:
using System;
using System.Runtime.InteropServices;
using System.Threading; // <- added this line
我将这些行添加到重试处理程序中:
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;
}
此外,如果用户碰巧碰到键盘或点击了一个可见的应用程序,Excel会连续抛出RPC_E_CALL_REJECTED
,除非您在将应用程序实例设置为可见之前调用它。
$Application.Interactive = $false
在获取引用的脚本后,您的代码变为:
$a = New-Object -ComObject Excel.Application
AddMessageFilterClass
$Application.Interactive = $false
$a.Visible = $True
答案 1 :(得分:0)
仅供参考,例如,Visible
为$false
时也会发生这种情况:
New-Object -ComObject Excel.Application -Property @{Visible = $false}
另外,就我而言,它仅在通过WinRM启动时抛出错误;如果我在主机上执行了脚本,它就可以正常工作。