使用PowerShell中的参数启动Excel宏

时间:2015-07-06 12:39:39

标签: excel-vba powershell vba excel

我遇到以下PowerSehll代码的问题,以便启动Excel宏并传递参数。

# start Excel
$excel = New-Object -comobject Excel.Application

#open file
$FilePath = 'C:\Users\{user}\Desktop\test\Macro.xlsm'
$workbook = $excel.Workbooks.Open($FilePath)

$excel.Visible = $true

#access the Application object and run a macro
$app = $excel.Application

$sourcepath = 'C:\Users\{user}\Desktop\test\FileIn.csv'
$destinationpath = 'C:\Users\{user}\Desktop\test\FileOut.xls'

$app.Run('FormatUmsatzliste',$sourcepath,$destinationpath) 
$app.Quit() 

代码工作正常并且宏被执行并且也应该执行它。 但是,最后我收到以下错误消息,我不知道为什么。

据我了解,PowerShell并不喜欢这个参数。 我希望你能帮助我。

PS L:\> C:\Data\Untitled1.ps1
Exception calling "Run" with "3" argument(s): "Exception from HRESULT: 0x800A9C68"
At C:\Data\Untitled1.ps1:17 char:1
+ $app.Run('FormatUmsatzliste',$sourcepath,$destinationpath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException

OverloadDefinitions
System.Object Run(System.Object Macro, System.Object Arg1, System.Object Arg2, System.Object Arg3, System.Object Arg4, System.Object Arg5, System.Object Arg6, System.Object Arg7, System.Object Arg8, System.Object Arg9, System.Object Arg10, System.Object Arg11, System.Object Arg12, System.Object Arg13, System.Object Arg14, System.Object Arg15, System.Object Arg16, System.Object Arg17, System.Object Arg18, System.Object Arg19, System.Object Arg20, System.Object Arg21, System.Object Arg22, System.Object Arg23, System.Object Arg24, System.Object Arg25, System.Object Arg26, System.Object Arg27, System.Object Arg28, System.Object Arg29, System.Object Arg30)
System.Object _Application.Run(System.Object Macro, System.Object Arg1, System.Object Arg2, System.Object Arg3, System.Object Arg4, System.Object Arg5, System.Object Arg6, System.Object Arg7, System.Object Arg8, System.Object Arg9, System.Object Arg10, System.Object Arg11, System.Object Arg12, System.Object Arg13, System.Object Arg14, System.Object Arg15, System.Object Arg16, System.Object Arg17, System.Object Arg18, System.Object Arg19, System.Object Arg20, System.Object Arg21, System.Object Arg22, System.Object Arg23, System.Object Arg24, System.Object Arg25, System.Object Arg26, System.Object Arg27, System.Object Arg28, System.Object Arg29, System.Object Arg30)

最好的问候。

1 个答案:

答案 0 :(得分:0)

如果您通过Excel.Application COM对象启动它,解决方案是不要在宏中使用Workbook.Close。此处详细介绍了此问题:HRESULT 0x800A9C68 in Excel 2010。解决方案是从正在调用的宏中删除对 array (size=2) 0 => object(stdClass)[27] public 'ID' => string '1' (length=1) public 'sectiontitle' => string 'Personal Information' (length=20) public 'field_format' => string 'vertical' (length=8) public 'status' => string '1' (length=1) 的调用。