vba 关闭最后一个文档后关闭应用程序(Acrobat、Word、Excel)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13705590/
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
Closing application (Acrobat, Word, Excel) after closing the last document
提问by dan
I have a few functions that generate Word, Excel or PDF documents. For example, the one I'm working on is exporting a report to a PDF file. After closing the report, it opens another PDF form created with LiveCycle ES 8.2 and it fills it with data from a database. After this, the document is closed, but for some reason, the instance of Acrobat is still open in foreground with no document opened.
我有几个函数可以生成 Word、Excel 或 PDF 文档。例如,我正在处理的一项工作是将报告导出为 PDF 文件。关闭报告后,它会打开另一个用 LiveCycle ES 8.2 创建的 PDF 表单,并用数据库中的数据填充它。此后,文档关闭,但由于某种原因,Acrobat 实例仍然在前台打开,没有打开文档。
Here is the code:
这是代码:
DoCmd.OpenReport "myReport", acViewPreview
DoCmd.OutputTo acOutputReport, "", acFormatPDF, "C:\myReport.pdf", False
DoCmd.Close acReport, "myReport"
Dim gApp, avDoc, pdDoc, jso
Set gApp = CreateObject("AcroExch.app")
Set avDoc = CreateObject("AcroExch.AVDoc")
If avDoc.Open(exprPDF, "") Then
Set pdDoc = avDoc.GetPDDoc()
Set jso = pdDoc.GetJSObject
'[...]
pdDoc.Save PDSaveIncremental, FileNm 'Save changes to the PDF document
pdDoc.Close 'Close the PDF document
End If
avDoc.Close (True)
Set gApp = Nothing
Set avDoc = Nothing
myReport never opens any instance of Acrobat. The line avDoc.Open
does. I would like Acrobat to be closed when I do avDoc.close
. Any ideas?
myReport 从不打开任何 Acrobat 实例。线路avDoc.Open
可以。当我这样做时,我希望 Acrobat 关闭avDoc.close
。有任何想法吗?
采纳答案by dan
After some more tests, I need to both close and quit all document objects to ensure that the app quits if there is no remaining opened document.
经过更多测试后,我需要关闭并退出所有文档对象,以确保在没有剩余打开的文档时应用程序退出。
回答by blablubbb
I think you should add:
我认为你应该补充:
gApp.Exit
before the
之前
Set gApp = Nothing
That seems to do the tick, but the app is still there, hidden... To Actually kill Acrobat try this method here with "Acrobat.exe": How can I kill task manager processes through VBA code?That method is a bit of a sledge hammer, but it works.
这似乎可以解决问题,但该应用程序仍然存在,隐藏...要实际杀死 Acrobat,请在此处使用“Acrobat.exe”尝试此方法: 如何通过 VBA 代码杀死任务管理器进程?这种方法有点像大锤,但它有效。
回答by Andy Bird
I have just experienced the same issue and found the following solution:
我刚刚遇到了同样的问题,并找到了以下解决方案:
AVDoc.Close (True)
**AcroApp.Hide**
Set AcroApp = Nothing
Set AVDoc = Nothing