在 powershell 中打开 excel 时出错

Error opening excel in powershell

我需要使用 powershell 脚本中的 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

我不知道为什么会出现错误。我很乐意接受任何建议和假设!

经过多次使用您的 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)

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 个参数提供什么值,它都将无法打开文件。

郑重声明,它适用于 14 个或更少的参数,只有第 15 个参数抛出异常。

结论

根据我所看到的一切,我只能得出结论,powershell 中的 Excel COM 互操作支持存在问题。提供第 15 个参数作为 $missing 根本不应该根据 Workbooks.Open() reference 改变行为。 Powershell 的 Excel COM 支持存在问题,因为当 VBA 脚本作为宏 运行 时,一切都按照文档进行。

建议的后续步骤

想到的第一个解决方法是将 VBA 脚本编写为宏,并将其存储在 excel 文件中,该文件专门用于 运行ning VBA 来自命令行的脚本。实施起来只需很少的努力,并且通过 OP 和我自己的测试知道它会起作用。

如果您在从 powershell 触发宏时遇到任何困难,请参阅 Calling Excel macros from PowerShell with arguments