Chr*_*ris 8 powershell powerpoint vba powerpoint-vba
试图将标题串起来就足以成为一种挑战......
我试图从PowerShell运行一些PowerPoint宏.我已经非常擅长从Powershell for Excel运行宏.当我在Excel上运行宏时,COM对象的Run()方法将采用各种参数,具体取决于宏是否有任何参数.但另一方面,PowerPoint Run()方法需要参数,我无法弄清楚如何传递它们.
我的宏期待通过一个字符串,我已经大量搜索并且缩短了.我总是得到这个错误:
错误:
type must not be ByRef
Run Code Online (Sandbox Code Playgroud)
我在PowerShell中整理了一个非常基本的PoC for PowerPoint:
码:
# 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
Run Code Online (Sandbox Code Playgroud)
宏本身只是在框中移动一些文本,从PowerPoint运行时它可以正常工作.
需求:
我现在想要跨多个文件和幻灯片自动执行此操作.
有关PowerPoint COM对象的文档运行方法,显示了两个参数的要求.
很好的问题,但正如你所说,网上的例子并不多。我设法进一步简化您的示例,并成功地将一些文本传递到 PowerPoint 宏中的 MsgBox,而无需真正更改您所拥有的内容。
PowerShellTest.pptm 文件中的宏保存在 C:\Temp 中
Sub DisplayMessage(myText As String)
MsgBox myText
End Sub
Run Code Online (Sandbox Code Playgroud)
PowerShell 脚本:
# 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")
Run Code Online (Sandbox Code Playgroud)
您提供的 Run 方法文档链接提到可能包含模块名称,因此这也对我有用:
$PowerPoint.run("PowerShellTest.pptm!Module1.DisplayMessage",[ref]"Feb")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
999 次 |
| 最近记录: |