错误在PowerShell中打开Excel

我需要打开从CorruptLoad脚本CorruptLoad参数的Excel文件。 但是,当我尝试做到这一点,我得到一个错误Exception calling "Open" with "15" argument(s): "open method workbooks class failed"只有当我用全部15个参数调用Open时, 才会出现此错误。 而当我尝试用15个参数或指定值的命名参数CorruptLoadVB.net程序打开相同的Excel文件没有问题!

我正在使用powershell v 4.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 

我不知道为什么发生错误。 我会很乐意提供任何build议和假设!

经过与你的PowerShell脚本玩了很多…这都是非常奇怪的。

观察到的行为

首先,运行$excel.Workbooks.Open.Invoke.ToString()时,Workbooks对象上的Open方法仅报告14个参数。 ouptut写道:

 Workbook Open (string, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant) 

然而,有15种参数的重载方法,因为当使用更详细的错误读出$Error[0]|format-list -force我做了两个testing调用,第一个用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) 

16个参数

 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个参数的值是什么,都无法打开文件。

对于logging,它使用14或更less的参数,它只是第15个参数抛出exception。

结论

从我见过的所有事情中,我只能得出结论,PowerShell中的Excel COM互操作支持存在问题。 提供第15个参数为$missing根本不应该根据Workbooks.Open()引用更改行为。 Powershell的Excel COM支持问题进一步加剧,因为当VBA脚本以macros的forms运行时,所有内容都按照文档运行。

build议的后续步骤

想到的第一个解决方法是将VBA脚本编写为macros,并将其存储在专门用于从命令行运行VBA脚本的excel文件中。 实施起来只需要很less的工作,而且OP和我自己的testing都知道它会起作用。

如果从powershell触发macros有任何困难,请参阅使用参数从PowerShell调用Excelmacros