使用Excel VBAclosuresPowerpoint对象(不使用Powerpoint.Application)

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

我使用对象。 它工作很好。 除了一件:closuresPowerpoint。

码:

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 

活动的演示文稿将closures,但Powerpoint本身保持打开状态(没有文件窗口打开)。 然后,因为它是开放的,当下一个运行(我有一个循环,将循环,并做许多这些build立背靠背),它打开了模板以及最新的内置Powerpoint文件,创build系统locking问题。

有任何想法吗?

非常感谢您的帮助!

我不完全确定你的代码不工作。 我试图设置oPPTPres = Nothingbuild议也没有工作。 但是,下面的方法PowerPoint在我的电脑上closures

 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 

JMP,

Sean在从内存中删除对象方面是正确的,但是您还需要确保释放任何和所有直接引用到您的Powerpoint对象,以防将指针存储在其他variables中。 值得注意的是,这不会杀死应用程序,并停止线程 – 它将简单地释放您的应用程序variables。

保罗Bclosurespowerpoint的方法应该可以正常工作,而这篇文章有一个closures应用程序的软方法和暴力方法 。

我调整和testing了这个简单的暴力方法在我的机器上从Excel相对权限限制的设置,并立即杀死了Powerpoint应用程序:

 Sub ForcePowerpointExit() Dim BruteForce As String BruteForce = "TASKKILL /F /IM powerpnt.exe" Shell BruteForce, vbHide End Sub 

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

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

我相信所有其他海报至less部分是正确的。 保罗·B的答案应该在大多数情况下工作。

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

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

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

    Interesting Posts