使用参数从PowerShell调用Excel宏

Bob*_*sen 10 excel powershell vba

使用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()
Run Code Online (Sandbox Code Playgroud)

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

Ans*_*ers 8

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

$excel.Run('CreateChart', 'arg1', 'arg2', ...)
Run Code Online (Sandbox Code Playgroud)

  • 我不得不在宏上设置名称如下`ThisWorksheet.CreateChart`,所以你的代码变成了`$excel.Run('ThisWorkbook.CreateChart', 'arg1', 'arg2', ...)` (2认同)