[英]Run Powerpoint sub from Excel VBA
I need to run a PowerPoint sub from a sub in Excel.我需要从 Excel 中的子程序运行 PowerPoint 子程序。 The reason is that most PowerPoint actions run far faster and with less errors when run from a sub in PowerPoint than when run from a sub in Excel.
原因是大多数 PowerPoint 操作在从 PowerPoint 中的子程序运行时比从 Excel 中的子程序运行时运行得更快并且错误更少。
I am trying to use Application.Run(Macroname_As_String)
where I use PptApp
in place of Application
and PptApp
is loaded as:我正在尝试使用
Application.Run(Macroname_As_String)
,其中我使用PptApp
代替Application
并且PptApp
加载为:
Dim PptApp As PowerPoint.Application
Set PptApp = CreateObject("PowerPoint.Application")
I tried referring to the VBA script as both Presentation1.pptm!UpdateOLELinks
and UpdateOLELinks
ie.我尝试将 VBA 脚本称为
Presentation1.pptm!UpdateOLELinks
和UpdateOLELinks
即。 file and VBA script / just VBA script.文件和 VBA 脚本/只是 VBA 脚本。
I get the error :我收到错误:
"Method 'Run' of object '_Application' failed".
“对象‘_Application’的方法‘运行’失败”。
My VBA script UpdateOLELinks
is located in Module1
of Presentation1
.我的 VBA 脚本
UpdateOLELinks
位于Presentation1
Module1
中。
Any Ideas?有任何想法吗?
The Run Method in PowerPoint require parameters : msdn.microsoft.com/fr-fr/library/office/Ff744221.aspx PowerPoint中的 “ 运行方法”需要参数 : msdn.microsoft.com/fr-fr/library/office/Ff744221.aspx
So, even if you pass an empty array, try something like : 因此,即使您传递了一个空数组,也可以尝试执行以下操作:
PptApp.Run Macroname_As_String, Parameters_As_Array
Other untested possibilities (with your references for context) I stumbled across while researching : 在研究时,我偶然发现了其他未经检验的可能性(有您的参考资料供参考):
Dim PptApp As PowerPoint.Application
Set PptApp = CreateObject("PowerPoint.Application")
Set Ppt1 = PptApp.Presentations.Open(PptPath, msoFalse, msoTrue, msoTrue)
'Possibility 1
PptApp.Run Macroname_As_String, Parameters_As_Array
'Possibility 2
Ppt1.PptApp.Run Macroname_As_String, Parameters_As_Array
'Possibility 3
PptApp.Run "'" & Ppt1.name & "'!" & Macroname_As_String, Parameters_As_Array
'Possibility 4
PptApp.Run Module_Name.Macroname_As_String, Parameters_As_Array
'Possibility 5
PptApp.Run "'" & Ppt1.name & "'!" & Module_Name.Macroname_As_String, Parameters_As_Array
I found the answer here, where "UpdateOLELinks" is the name of the PowerPoint sub and the option to compile live as you type has not been disabled (it's enabled by default): https://www.ozgrid.com/forum/forum/other-software-applications/excel-and-or-powerpoint-help/26816-open-ppt-and-run-a-pre-written-macro 我在这里找到了答案,其中“ UpdateOLELinks”是PowerPoint子名称,并且在键入时进行实时编译的选项未被禁用(默认情况下处于启用状态): https : //www.ozgrid.com/forum/forum /其他软件的应用程序/ Excel的和有或PowerPoint中的帮助/ 26816-开PPT和运行-A-预先写好的宏
from ASHOK_SHARMA02: 从ASHOK_SHARMA02:
Dim PPApp As PowerPoint.Application
Set PPApp = CreateObject("PowerPoint.Application")
PPApp.AddIns.Application.Run ("UpdateOLELinks"), ""
It worked for me after trying loads of possible solutions. 在尝试了大量可能的解决方案之后,它为我工作。
[edit] Actually it broke again when running the PPT from the VBA. [edit]实际上,当从VBA运行PPT时,它再次崩溃。 Reason is VBA module has not yet been activated, so something like PPT doesn't know it exists (crazy huh?).
原因是VBA模块尚未激活,所以类似PPT的东西不知道它存在(疯狂吧?)。 So, add line
因此,添加行
PPApp.VBE.ActiveVBProject.VBComponents.Item("Module1").Activate
There are two issues (which seem unique to PowerPoint), parameters are required and the macro name must be fully qualified. 有两个问题(对于PowerPoint来说似乎是唯一的),需要参数,并且宏名称必须完全合格。
When qualifying the macro, don't use single quotes as you would for Excel. 限定宏时,请勿像对Excel那样使用单引号。 Instead, just use the following, even if the filename has spaces:
而是,即使文件名包含空格,也请使用以下命令:
PptApp.Run Ppt1.Name & "!Module1.UpdateOLELinks"
The error will also arise if the parameters being passed don't match the parameters of the macro. 如果传递的参数与宏的参数不匹配,也会出现该错误。 Ensure the macro has a defined parameter to receive (of matching type), even if it doesn't use it.
确保宏具有定义的参数来接收(匹配类型),即使它不使用它也是如此。
This has been asked long before but still if anyone needs an answer, the follwoing worked for me.很久以前就有人问过这个问题,但如果有人需要答案,以下内容对我有用。
Dim objPP As Object
Dim objPPFile As Object
Set objPP = CreateObject("PowerPoint.Application")
objPP.Visible = True
Set objPPFile = objPP.Presentations.Open(PptPath)
Application.EnableEvents = False
' "filename !Module1.macro_name"
objPP.Run "post_processing_V2.pptm!Module1.code"
objPPFile.Close
objPP.Quit
Set objPPFile = Nothing
Set objPP = Nothing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.