首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Excel VBA关闭Powerpoint对象(不使用Powerpoint.Application)

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

Stack Overflow用户
提问于 2012-08-29 00:14:15
回答 4查看 32.6K关注 0票数 5

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

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

代码:

代码语言:javascript
复制
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

。。。。

代码语言:javascript
复制
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文件,从而产生系统锁定问题。

有什么想法吗?

非常感谢您的帮助!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-08-29 02:06:34

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

代码语言:javascript
复制
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)

..。

代码语言:javascript
复制
oPPTPres.Close
Set oPPTPres = Nothing
oPPTApp.Quit
Set oPPTApp = Nothing
票数 6
EN

Stack Overflow用户

发布于 2012-08-29 02:08:52

JMP,

Sean在从内存中删除对象方面是正确的,但你也需要确保release any and all direct references到你的powerpoint对象,以防你在其他变量中存储指向powerpoint的指针。然而,值得注意的是,这不会杀死应用程序并停止线程-它只是释放应用程序变量。

Paul B的关闭powerpoint的方法应该工作得很好,这个SO Article有一个软方法和一个关闭应用程序的brute-force method,如果它们留在内存中的话。

我将这个简单的bruteforce方法在我机器上的相对权限有限的设置上进行了调整和测试,它立即终止了Powerpoint应用程序:

代码语言:javascript
复制
Sub ForcePowerpointExit()


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

Shell BruteForce, vbHide

End Sub

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

票数 4
EN

Stack Overflow用户

发布于 2013-05-20 07:19:49

我相信其他的帖子至少是部分正确的。Paul B.的答案在大多数情况下都应该是有效的。

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

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12163390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档