【问题标题】:Error opening excel in powershell在 powershell 中打开 excel 时出错
【发布时间】:2016-05-28 21:29:04
【问题描述】:

我需要使用来自 powershell-script 的 CorruptLoad 参数打开 excel 文件。但是当我尝试实现它时,我收到一个错误Exception calling "Open" with "15" argument(s): "open method workbooks class failed"。此错误在我使用所有 15 个参数调用 Open 时发生。而当我尝试用VB.net 15 个参数或指定命名参数CorruptLoad 的值的程序打开同一个excel 文件时,没有问题!

我正在使用 powershell v 4.0、带有 SP2 的 Office 2010 和 .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

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

【问题讨论】:

  • 如果需要使用CorruptLoad,为什么没有在参数列表中指定呢?你有$missing吗? msdn.microsoft.com/en-us/library/office/ff193596.aspx.
  • @Matt,我尝试了调用Open 函数的所有变体。我更改了我的代码示例以显示它。
  • 感谢您的澄清。

标签: excel powershell powershell-4.0


【解决方案1】:

在玩了很多 PowerShell 脚本之后……这一切都很奇怪。

观察到的行为

首先,Workbooks 对象的 Open 方法在运行 $excel.Workbooks.Open.Invoke.ToString() 时只报告 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 脚本编写为宏并将其存储在专门用于从命令行运行 VBA 脚本的 excel 文件中。实施起来只需要很少的努力,并且通过 OP 和我自己的测试都知道它会起作用。

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

【讨论】:

  • 彼得,感谢您对另一个文件中的宏的想法!而使用 Excel COM 组件有一个有趣的情况:在另一台服务器上使用Office 2010 SP2、powershell 2.0 和.NET Framework 4.0 不会出现此类问题。
  • @Indian 我没有任何可比较的环境,所以我无法确认这一点。我们确实有相当多的错误证据;因此,如果您愿意,可以在此处将其提交给 Microsoft:connect.microsoft.com/PowerShell 我的回答中的信息应该为错误报告提供了良好的基础。
猜你喜欢
  • 1970-01-01
  • 2011-08-14
  • 2023-04-10
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
相关资源
最近更新 更多