在powershell中打开excel时出错
我需要使用powershell脚本中的在powershell中打开excel时出错,excel,powershell,powershell-4.0,Excel,Powershell,Powershell 4.0,我需要使用powershell脚本中的CorruptLoadparameter打开excel文件。但是,当我尝试执行此操作时,调用带有“15”参数的“Open”时出现错误异常:“Open方法工作簿类失败”。仅当我使用所有15个参数调用Open时,才会发生此错误。当我尝试用带有15个参数的VB.net程序打开同一个excel文件,或者指定命名参数的值时,CorruptLoad,没有问题 我正在使用powershell V4.0、Office 2010和SP2以及.NET Framework 4.5
CorruptLoad
parameter打开excel文件。但是,当我尝试执行此操作时,调用带有“15”参数的“Open”时出现错误异常:“Open方法工作簿类失败”
。仅当我使用所有15个参数调用Open
时,才会发生此错误。当我尝试用带有15个参数的VB.net程序打开同一个excel文件,或者指定命名参数的值时,CorruptLoad
,没有问题
我正在使用powershell V4.0、Office 2010和SP2以及.NET Framework 4.5.2
这是我的powershell
代码:
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
try
{
$missing = [System.Type]::Missing
# $wb = $excel.Workbooks.Open("d:\temp\start_instrument.xls", $missing, $missing, $missing, $missing,
# $missing, $missing, $missing, $missing, $missing,
# $missing, $missing, $missing, $missing, $missing)
# $wb = $excel.Workbooks.Open("d:\temp\start_instrument.xls", $missing, $missing, $missing, $missing,
# $missing, $missing, $missing, $missing, $missing,
# $missing, $missing, $missing, $missing, 1)
$XlCorruptLoad = "Microsoft.Office.Interop.Excel.XlCorruptLoad" -as [type]
$wb = $excel.Workbooks.Open("d:\temp\start_instrument.xls", $missing, $missing, $missing, $missing,
$missing, $missing, $missing, $missing, $missing,
$missing, $missing, $missing, $missing, $XlCorruptLoad::xlRepairFile)
}
catch
{
Write $Error[0].ToString()
}
# some stuff
if ($excel -ne $null)
{
$excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
$excel = $null
}
[System.GC]::Collect() | Out-Null
[System.GC]::WaitForPendingFinalizers() | Out-Null
我不知道为什么会发生错误。我很乐意接受任何建议和假设 在反复使用PowerShell脚本之后。。。这一切都很奇怪 观察到的行为 首先,当运行
$excel.Workbooks.Open.Invoke.ToString()
时,Workbooks对象上的Open方法仅报告14个参数。该文件内容如下:
Workbook Open (string, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant)
但是,对于15个参数有某种重载方法,因为当使用更详细的错误读取$error[0]| format list-force时,我进行了两次测试调用,第一次调用15个参数,第二次调用16个参数
15个参数
Exception : System.Runtime.InteropServices.COMException (0x800A03EC): Unable to get the Open property of the Workbooks class
at System.Management.Automation.Interpreter.MethodInfoCallInstruction.InvokeInstance(Object instance, Object[] args)
at System.Management.Automation.Interpreter.DynamicInstructionN.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Exception : System.Management.Automation.MethodException: Cannot find an overload for "Open" and the argument count: "16" --->
System.Reflection.TargetParameterCountException: Cannot find an overload for "Open" and the argument count: "16"
--- End of inner exception stack trace ---
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception
exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
16个参数
Exception : System.Runtime.InteropServices.COMException (0x800A03EC): Unable to get the Open property of the Workbooks class
at System.Management.Automation.Interpreter.MethodInfoCallInstruction.InvokeInstance(Object instance, Object[] args)
at System.Management.Automation.Interpreter.DynamicInstructionN.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Exception : System.Management.Automation.MethodException: Cannot find an overload for "Open" and the argument count: "16" --->
System.Reflection.TargetParameterCountException: Cannot find an overload for "Open" and the argument count: "16"
--- End of inner exception stack trace ---
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception
exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
如上所述,该方法支持15个参数,但不支持16个参数。但是,无论为第15个参数提供什么值,它都无法打开文件
作为记录,它使用14个或更少的参数,只有第15个参数抛出异常
结论
从我所看到的一切来看,我只能断定powershell中的Excel COM互操作支持存在问题。将第15个参数提供为$missing
不应根据参数改变行为。Powershell的Excel COM支持存在一个问题,这一点得到了进一步加强,因为当VBA脚本作为宏运行时,一切都按照文档进行
建议的下一步行动
首先想到的解决方法是将VBA脚本编写为宏,并将其存储在excel文件中,该文件专门用于从命令行运行VBA脚本。它只需要很少的努力就可以实现,而且OP和我自己的测试都表明它可以工作
如果从powershell触发宏时遇到任何困难,请参见如果需要使用CorruptLoad
为什么没有在参数列表中指定它?您将其命名为$missing
@Matt,我尝试了调用Open
函数的所有变体。我更改了代码示例以显示它。感谢您的澄清。Peter,感谢您对另一个文件中的宏的想法!使用Excel COM组件时,会出现有趣的情况:在另一台具有Office 2010
SP2、powershell 2.0和.NET Framework 4.0
的服务器上,不会出现此类问题。@我没有任何类似的环境,因此无法确认这一点。我们确实有相当多的证据表明存在一个bug;因此,如果您愿意,您可以在这里将其提交给Microsoft:我的回答中的信息应该为错误报告提供良好的基础。