通过PowerShell通过COM对象传递变量以在PowerPoint中运行宏 [英] Passing a variant through COM object via PowerShell to run a macro in PowerPoint

查看:120
本文介绍了通过PowerShell通过COM对象传递变量以在PowerPoint中运行宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图将标题串在一起就足够了……

Trying to string the title together was enough of a challenge...

我正在尝试从PowerShell运行一些PowerPoint宏。我已经非常擅长从Powershell for Excel运行宏。当我在Excel上运行宏时,COM对象中的Run()方法将采用各种参数,具体取决于宏是否具有任何参数。但是另一方面,PowerPoint Run()方法需要参数,而我无法弄清楚如何传递它们。

I am trying to run some PowerPoint macros from PowerShell. I have gotten quite good at running macros from Powershell for Excel. When I run the macros on Excel, the Run() method from the COM object will take a variety of arguments, depending if the macro has any parameters. However on the other hand, the PowerPoint Run() method expects parameters, and I cannot work out how to pass them.

我的宏期望通过一个字符串,我已经在Google上进行了大量搜索,但结果却很简短。我总是会收到此错误:

My macro is expecting one string to be passed through, I've googled profusely and come up short. I always get this error:

错误:

type must not be ByRef

我在PowerShell中为PowerPoint编写了非常基本的PoC :

I have put together a very basic PoC for PowerPoint in PowerShell:

代码:

# 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中运行时效果很好。

The macro itself just moves some text across boxes, it works fine when run from PowerPoint.

要求评论:

我现在要在多个文件和幻灯片中实现自动化。

I now want to automate this across multiple files and slides.

有关PowerPoint COM对象运行方法的文档,其中显示了两个参数的要求。

Documentation on the PowerPoint COM object Run method, showing the requirement for two parameters.

推荐答案

这个好问题,网上说的例子并不多。我设法进一步简化了示例,并成功地将一些文本传递到PowerPoint宏中的MsgBox,而没有真正更改您的内容。

Great question and not a lot of examples online as you say. I managed to strip your example down even further and successfully pass some text to a MsgBox in a PowerPoint Macro without really changing what you had.

PowerShellTest文件中的宏。 pptm保存在C:\Temp

The Macro in the file PowerShellTest.pptm saved in C:\Temp

Sub DisplayMessage(myText As String)
  MsgBox myText
End Sub

PowerShell脚本:

The PowerShell script:

# PowerPoint test
Add-type -AssemblyName office
$PowerPoint = New-Object -comobject PowerPoint.Application

$PowerPoint.Visible = [Microsoft.Office.Core.MsoTriState]::msoTrue

$presentation = $PowerPoint.Presentations.open("C:\Temp\PowerShellTest.pptm")

$PowerPoint.run("PowerShellTest.pptm!DisplayMessage",[ref]"Feb")

您提供的运行方法文档链接提到可能包含模块名称,因此它也对我有用:

The Run method documentation link you provided mentions that the module name may be included so this worked for me too:

$PowerPoint.run("PowerShellTest.pptm!Module1.DisplayMessage",[ref]"Feb")

这篇关于通过PowerShell通过COM对象传递变量以在PowerPoint中运行宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆