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

时间:2012-08-28 16:14:15

标签: excel vba object powerpoint

希望有人可以帮我处理一些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

。 。 。

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

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

有什么想法吗?

非常感谢你的帮助!

4 个答案:

答案 0 :(得分:4)

我不完全确定您的代码无效。我尝试按照建议设置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

答案 1 :(得分:4)

JMP,

Sean在从内存中删除对象方面是正确的,但是如果你将指针存储在其他变量的powerpoint中,你还需要确保{power}对象release any and all direct references。但值得注意的是,这不会杀死应用程序并停止线程 - 它只会释放您的应用程序变量。

保罗B关闭powerpoint的方法应该可以正常工作,如果应用程序保留在内存中,这个SO Article有一个软方法和brute-force method关闭应用程序。

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

Sub ForcePowerpointExit()


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

Shell BruteForce, vbHide

End Sub

因此,它为您提供了另一种杀死应用程序的选项。

答案 2 :(得分:1)

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

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

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

将所有VBA powerpoint代码移至模块并在启动自动化(powerpoint)代码之前隐藏userform。

答案 3 :(得分:0)

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

相关问题