处置 Microsoft.Office.Interop.Word.Application
(该帖子的后续内容(仍未得到答复):https://stackoverflow.com/q/6197829/314661)
使用以下代码,
Application app = new Application();
_Document doc = app.Documents.Open("myDocPath.docx", false, false, false);
doc.PrintOut(false);
doc.Close();
我尝试以编程方式打开和打印文件。
问题是每次我运行上面的代码时都会启动一个新的 WINWORD.exe 进程,显然这很快就会耗尽所有内存。
应用程序类似乎不包含 dispose/close 或类似的方法。
经过一番研究后,我(意识到)并将代码更改为以下内容。
Application app = new Application();
_Document doc = app.Documents.Open(fullFilePath + ".doc", false, false, false);
doc.PrintOut(false);
doc.Close();
int res = System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
int res1 = System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
我可以看到剩余的引用计数为零,但进程仍然存在?
PS:我使用的是 Microsoft.Office.Interop 库的版本 14。
(Somewhat of a follow on from the post (which remains unanswered): https://stackoverflow.com/q/6197829/314661)
Using the following code
Application app = new Application();
_Document doc = app.Documents.Open("myDocPath.docx", false, false, false);
doc.PrintOut(false);
doc.Close();
I am attempting to open and print a file programmatically.
The problem is each time I run the above code a new WINWORD.exe process is started and obviously this quickly eats up all the memory.
The application class doesn't seem to contain a dispose/close or similar method.
After a bit of research I (realized) and changed the code to the following.
Application app = new Application();
_Document doc = app.Documents.Open(fullFilePath + ".doc", false, false, false);
doc.PrintOut(false);
doc.Close();
int res = System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
int res1 = System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
And I can see the remaining reference count is zero but the processes remain?
PS: I'm using Version 14 of the Microsoft.Office.Interop library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您不需要调用
Quit
吗?Do you not need to call
Quit
?也许尝试设置
doc = null
并调用GC.Collect()
编辑,不是我自己的代码,我忘记了从哪里得到它,但这是我用来处理 Excel 的,它完成了工作,也许你可以从中收集到一些东西:
Perhaps try setting
doc = null
and callingGC.Collect()
Edit, not really my own code I forget where I got it but this is what I use to dispose of Excel, and it does the job maybe you can glean something from this:
我认为似乎没有人注意到的主要问题是,如果 Word 已经打开,那么您不应该首先创建新的应用程序对象。
我们这些从 COM 和/或 VB6 时代就开始编码的人会记得 GetActiveObject。幸运的是.Net只需要一个ProgID。
推荐的方法如下:
I think the main issue, which nobody seems to have picked up on, is that you shouldn't be creating a new Application object in the first place if Word is already open.
Those of us who have been coding since the days of COM and/or VB6 will remember GetActiveObject. Fortunately .Net only requires a ProgID.
The recommended way of doing this is as follows:
最好的解决方案..最后:
The best solution.. last:
您需要调用
app.Quit()
来关闭应用程序。我使用下面的代码&它对我来说就像一种魅力 -You need to calls
app.Quit()
to close the application. I used below code & it worked like a charm for me -同意其他发帖者的观点,即不需要
GC.Collect()
和Marshal.ReleaseComObject()
。如果运行app.Quit(false)
后该进程仍然存在,可能是因为您正在以不可见的方式运行应用程序,并且存在阻止应用程序关闭的提示,例如 Document恢复对话框。如果是这种情况,您需要在创建应用程序时添加此内容。Agreed with other posters that
GC.Collect()
andMarshal.ReleaseComObject()
is not needed. If the process still exists after runningapp.Quit(false)
, it might be because you're running the app invisible, and there is a prompt that is preventing the application from closing, such as a Document Recovery dialog. If that's the case, you need to add this when creating your application.我关闭文档,然后关闭适合我的应用程序,然后强制垃圾回收。
I close the document, then the application, that works for me, then force garbage collection.
试试这个..
Try this..
就我而言,这是因为我调用了
wordApp.Quit()
而不是wordApp.Quit(false)
。刚刚添加了false
来调用此方法,现在它工作正常。In my case it was because i called
wordApp.Quit()
instead ofwordApp.Quit(false)
. Just addedfalse
to call of this method and now it works fine.