从 PowerShell 运行 PowerPoint 宏 [英] Run PowerPoint Macro from PowerShell
问题描述
我有一个带有以下宏的 PowerPoint:
I have a PowerPoint with the following macro:
Sub test()
MsgBox "testing"
End Sub
还有一个像这样的 PowerShell 脚本:
And a PowerShell script like this:
$ppt = New-Object -ComObject PowerPoint.Application
$presentation = $ppt.Presentations.Open("test.pptm")
$ppt.Run("test")
但是运行宏只会给出:
Cannot find an overload for "Run" and the argument count: "1".
At line:1 char:1
+ $ppt.Run("test")
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
我遇到同样的错误,例如$presentation.application.run("test")
和$ppt.Run("test.pptm!test")
.
I get the same error for e.g. $presentation.application.run("test")
and
$ppt.Run("test.pptm!test")
.
相关:
通过通过 PowerShell 通过 COM 对象的变体在 PowerPoint 中运行宏
文档建议 Run
应该只将宏名称作为字符串作为它的第一个参数,所以我看不出哪里出错了.
The documentation suggests that Run
should just take the macro name as a string as its first argument, so I can't see where I'm going wrong.
OverloadDefinitions
-------------------
System.Object Run(string MacroName, [ref] Params System.Object[] safeArrayOfParams)
System.Object _Application.Run(string MacroName, [ref] Params System.Object[] safeArrayOfParams)
Application.Run 方法 (PowerPoint)>
推荐答案
基于 这篇文章:
$ppt = New-Object -ComObject PowerPoint.Application
$presentation = $ppt.Presentations.Open("test.pptm")
$presname = $presentation.Name
$VBScript = New-Object -ComObject "MSScriptControl.ScriptControl"
$VBscript.Language = "VBScript"
$VBscript.AddCode(
@"
Function RunVBA( app, functionName )
RunVBA = app.Run( functionName, array() )
End Function
"@
)
$VBscript.CodeObject.RunVBA( $ppt, "$presname!test" )
请注意,这仅适用于 Powershell x86,因为 ScriptControl 在 64 位环境中不可用.
Note that this only works with Powershell x86, since ScriptControl is not available in a 64-bit environment.
这篇关于从 PowerShell 运行 PowerPoint 宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!