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

JMP*_*JMP 5 excel powerpoint vba object

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

我用对象.它很棒.除了一件:关闭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.


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

....

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
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗?

非常感谢您的帮助!

Pau*_* B. 6

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

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)
Run Code Online (Sandbox Code Playgroud)

...

oPPTPres.Close
Set oPPTPres = Nothing
oPPTApp.Quit
Set oPPTApp = Nothing
Run Code Online (Sandbox Code Playgroud)


Jac*_*ern 5

杰普,

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

Paul B 的关闭 powerpoint 的方法应该可以正常工作,并且这篇SO 文章有一个软方法和一个强力方法来关闭应用程序(如果它们保留在内存中)。

我通过 Excel 在我的计算机上相对权限受限的设置上调整并测试了这个简单的暴力破解方法,它立即终止了 Powerpoint 应用程序:

Sub ForcePowerpointExit()


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

Shell BruteForce, vbHide

End Sub
Run Code Online (Sandbox Code Playgroud)

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