vba 使用参数从 PowerShell 调用 Excel 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19536241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:01:00  来源:igfitidea点击:

Calling Excel macros from PowerShell with arguments

excelvbapowershell

提问by Bob Jansen

Using Powershell it's rather easy to call Excel macro's from a script, for example with a script like this:

使用PowerShell这是相当容易调用Excel宏的从脚本,例如与像一个脚本这样

$excel = new-object -comobject excel.application
$excelFiles = Get-ChildItem -Path C:\fso -Include *.xls, *.xlsm -Recurse
Foreach($file in $excelFiles)
{
   $workbook = $excel.workbooks.open($file.fullname)
   $worksheet = $workbook.worksheets.item(1)
   $excel.Run("CreateChart")
   $workbook.save()
   $workbook.close()
}
$excel.quit()

However, I didn't manage to call a macro with some arguments. Is this possible or is the best way to write a config file that the macro will read when called?

但是,我没有设法调用带有一些参数的宏。这是可能的还是编写宏将在调用时读取的配置文件的最佳方式?

采纳答案by Ansgar Wiechers

You can run a macro with arguments like this:

您可以使用如下参数运行宏:

$excel.Run('CreateChart', 'arg1', 'arg2', ...)