使用 Excel VBA 关闭 Powerpoint 对象(不使用 Powerpoint.Application)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12163390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:53:41  来源:igfitidea点击:

Close Powerpoint Object with Excel VBA (NOT using Powerpoint.Application)

excelvbaobjectpowerpoint

提问by JMP

Hoping someone can help me with some VBA code. I use a VBA loop to paste Excel charts, text boxes, and tables into a Powerpoint template. However, because I cannot be sure that the user will have the Powerpoint Object Library installed, I cannot use the Dim PPTApp as Powerpoint.Application type syntax.

希望有人可以帮助我一些 VBA 代码。我使用 VBA 循环将 Excel 图表、文本框和表格粘贴到 Powerpoint 模板中。但是,因为我不能确定用户是否会安装 Powerpoint 对象库,所以我不能使用 Dim PPTApp 作为 Powerpoint.Application 类型的语法。

I use objects. It works great. Except for one piece: closing Powerpoint.

我使用对象。它工作得很好。除了一件:关闭Powerpoint。

Code:

代码:

Dim oPPTPres As Object  ' Late binding: This is a PowerPoint.Presentation but we cannot assume that the Microsoft PowerPoint 11 library will be loaded in the workbook that this module has been copied to.
Dim oPPTShape As Object ' Late binding: This is a PowerPoint.Shapebut we cannot assume that the Microsoft PowerPoint 11 library will be loaded in the workbook that this module has been copied to.


PPTFile = Range("PPTFile").value ' Read PowerPoint template file name
Set oPPTPres = GetObject(PPTFile): oPPTPres.Application.Visible = msoTrue ' Switch to or open template file

. . . .

. . . .

strNewPresPath = Range("OutputFileName").value
oPPTPres.SaveAs strNewPresPath
' Range("PPTFile").value = strNewPresPath
ScreenUpdating = True
oPPTPres.Close 'Closes presentation but not Powerpoint
oPPTPres.Application.Quit 'No noticeable effect

The active presentation will close, but Powerpoint itself stays open (with no file window open). Then, because it is open, when the next one runs (I have a loop that will loop through and do many of these builds back-to-back), it opens up the template as well as the latest built Powerpoint file, creating system locking issues.

活动演示文稿将关闭,但 Powerpoint 本身保持打开状态(没有打开文件窗口)。然后,因为它是打开的,当下一个运行时(我有一个循环,它将循环并背靠背执行许多这些构建),它会打开模板以及最新构建的 Powerpoint 文件,创建系统锁定问题。

Any ideas?

有任何想法吗?

Thank you very much for your help!

非常感谢您的帮助!

采纳答案by Paul B.

I am not entirely sure why your code does not work. I tried to set oPPTPres = Nothingas suggested which did not work either. However, the following way PowerPoint closes on my computer

我不完全确定为什么您的代码不起作用。我尝试oPPTPres = Nothing按照建议进行设置,这也不起作用。但是,PowerPoint 以以下方式在我的计算机上关闭

Dim oPPTPres As Object  ' Late binding: This is a PowerPoint.Presentation but we cannot assume that the Microsoft PowerPoint 11 library will be loaded in the workbook that this module has been copied to.
Dim oPPTShape As Object ' Late binding: This is a PowerPoint.Shapebut we cannot assume that the Microsoft PowerPoint 11 library will be loaded in the workbook that this module has been copied to.
Dim oPPTApp As Object

Set oPPTApp = CreateObject("PowerPoint.Application")
oPPTApp.Visible = True

Set oPPTPres = oPPTApp.Presentations.Open(PPTFile)

...

...

oPPTPres.Close
Set oPPTPres = Nothing
oPPTApp.Quit
Set oPPTApp = Nothing

回答by HymanOrangeLantern

JMP,

JMP,

Sean is correct in terms of removing object from memory, but you need to make sure to release any and all direct referencesto your powerpoint object as well, in case you store the pointer to your powerpoint in other variables. Notably, however, this will not kill the application and stop the thread - it will simply deallocate your application variables.

Sean 在从内存中删除对象方面是正确的,但您需要确保释放对您的 powerpoint 对象的任何和所有直接引用,以防您将指向您的 powerpoint 的指针存储在其他变量中。然而,值得注意的是,这不会终止应用程序并停止线程 - 它只会释放您的应用程序变量。

Paul B's method of shutting down powerpoint should work fine, and this SO Articlehas a soft method and a brute-force methodof shutting down applications if they remain in memory.

保罗 B 的关闭 powerpoint 的方法应该可以正常工作,并且这篇SO 文章有一种软方法和一种蛮力方法,可以关闭保留在内存中的应用程序。

I adapted and tested this simple bruteforcemethod on relatively permissions-limited settings on my machine from Excel, and it killed the Powerpoint application immediately:

我在我的机器上从 Excel 中在相对权限受限的设置上调整并测试了这个简单的蛮力方法,它立即杀死了 Powerpoint 应用程序:

Sub ForcePowerpointExit()


Dim BruteForce As String
BruteForce = "TASKKILL /F /IM powerpnt.exe"

Shell BruteForce, vbHide

End Sub

So that provides you with another option for killing the application.

因此,这为您提供了另一种终止应用程序的选项。

回答by Anonymous Type

I believe all the other posters are at least partially correct. Paul B.'s answer should work in most cases.

我相信所有其他海报至少部分正确。保罗 B. 的答案应该在大多数情况下有效。

The only caveat will be if you have yor powerpoint VBA code being called directly from a the user form or an object that is directly referenced by the user form.

唯一需要注意的是,如果您有直接从用户表单或用户表单直接引用的对象调用您的 powerpoint VBA 代码。

In that case there is still a object reference waiting to be removed from memory.

在这种情况下,仍然有一个对象引用等待从内存中删除。

Move all your VBA powerpoint code to a module and hide the userform prior to kicking off the automation (powerpoint) code.

在启动自动化(powerpoint)代码之前,将所有 VBA PowerPoint 代码移动到一个模块并隐藏用户表单。

回答by SeanC

Set oPPTPres = Nothingshould remove the reference Excel has to the object, and (hopefully) release it from memory

Set oPPTPres = Nothing应该删除 Excel 对该对象的引用,并(希望)从内存中释放它