Error opening excel in powershell

扶醉桌前 提交于 2019-12-05 04:01:21
Marvin

After much playing around with your PowerShell script... it's all very odd.

Observed Behaviour

Firstly, the Open method on the Workbooks object only reports 14 parameters when $excel.Workbooks.Open.Invoke.ToString() is run. The ouptut reads:

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

However, there is some kind of overload method for 15 parameters because, when using the more verbose error read-out $Error[0]|format-list -force i did two test calls, the first with 15 parameters and the second with 16.

15 Parameters

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 Parameters

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)

As you can read above, the method does support 15 parameters but not 16. However, no matter what values are supplied to the 15th parameter, it will fail to open the file.

For the record, it works with 14 or less arguments, it is only the 15th argument that throws the exception.

Conclusion

From everything that I've seen, I can only conclude that there is a problem with the Excel COM interop support in powershell. Supplying the 15th parameter as $missing simply should not change behaviour according to the Workbooks.Open() reference. There being a problem with Powershell's Excel COM support is further reinforced because, when the VBA script is run as a macro, everything works as per the documentation.

Suggested Next Steps

The first work-around that comes to mind is to write the VBA script as a macro and store it in an excel file that is used specifically for running VBA scripts from the command line. It would take minimal effort to implement and it is known by testing from both the OP and myself that it will work.

If you have any difficulty triggering the macro from powershell, see Calling Excel macros from PowerShell with arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!