【发布时间】:2017-04-04 21:12:05
【问题描述】:
尝试将标题串在一起已经是一项挑战......
我正在尝试从 PowerShell 运行一些 PowerPoint 宏。我非常擅长从 Powershell for Excel 运行宏。当我在 Excel 上运行宏时,COM 对象的 Run() 方法将采用各种参数,具体取决于宏是否有任何参数。但是另一方面,PowerPoint Run() 方法需要参数,我不知道如何传递它们。
我的宏期望通过一个字符串,我在谷歌上大肆搜索,结果很短。我总是收到这个错误:
错误:
type must not be ByRef
我在 PowerShell 中为 PowerPoint 编写了一个非常基本的 PoC:
代码:
# PowerPoint test
Add-type -AssemblyName office
$PowerPoint = New-Object -comobject PowerPoint.Application
$PowerPoint.Visible = [Microsoft.Office.Core.MsoTriState]::msoTrue
$presentation2 = $PowerPoint.Presentations.open("C:\macros.pptm")
$presentation = $PowerPoint.Presentations.open("C:\Test For Macros.pptx")
$PowerPoint.run("macros.pptm!IAM",[ref]"Feb")
$presentation.save()
$presentation.close()
$presentation2.close()
$PowerPoint.quit()
# extra clean up omitted
宏本身只是跨框移动一些文本,从 PowerPoint 运行时效果很好。
要求:
我现在想跨多个文件和幻灯片自动执行此操作。
Documentation on the PowerPoint COM object Run method,表示需要两个参数。
【问题讨论】:
-
能不能把宏贴在pptm里(至少是sub的声明)?您能告诉我们其中包含哪些参考资料(工具 > 参考资料)吗?你能测试一下 (1) 你能在宏的开头显示一个消息框吗 (2) 可以从同一个文件中运行另一个具有相同类型参数的宏 (3) 可以从同一个文件中运行另一个不带参数的宏跑? (4) pptm 文件中的任何宏都可以在您计算机上的 PowerShell 中运行吗?
-
我之所以问这些,是因为我的假设是,当参数交给宏时不会发生错误,但稍后会抛出异常并最终被 PowerShell 捕获。
标签: vba powershell powerpoint