为什么在我对文档调用 Close() 后 Word 不释放我的文件?
我正在使用 MS Word COM API 从 C# 打印 Word 文档。请参见下文...
internal void PrintWordFileUsingDefaultPrinter(System.IO.FileInfo file)
{
//Open the document.
object fileName = file.FullName;
Document doc = app.Documents.Open(
ref fileName,
ref missing,
ref trueValue,
ref falseValue,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing);
//Send print job to the printer.
doc.PrintOut(
ref trueValue,
ref falseValue,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing);
doc.Close(ref falseValue, ref missing, ref missing);
}
您将看到我通过调用 doc.Close() 来完成。然而,即使在调用此 Word 后,仍然锁定我的文件,我无法进一步处理它。知道如何强制 word 释放我的文件吗?
(除了关闭Word进程本身?我不想这样做,因为我需要打印大量文档并且不喜欢每次都重新打开Word)
I am using the MS Word COM API to print Word documents from C#. See below...
internal void PrintWordFileUsingDefaultPrinter(System.IO.FileInfo file)
{
//Open the document.
object fileName = file.FullName;
Document doc = app.Documents.Open(
ref fileName,
ref missing,
ref trueValue,
ref falseValue,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing);
//Send print job to the printer.
doc.PrintOut(
ref trueValue,
ref falseValue,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing);
doc.Close(ref falseValue, ref missing, ref missing);
}
You will see that I finish by calling doc.Close(). However even after calling this Word still locks my file and I am unable to process it further. Any idea how I can force word to release my file?
(Apart from closing the Word process itself? I don't want to do this as I need to print a HUGE number of documents and prefer not to re-open Word every time)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须调用 Marshal.ReleaseComObject 对于文档的实例,能够释放它。
例如
Marshal.ReleaseComObject(doc);
您还需要释放
Word.Application
实例以从内存中释放Word 应用程序实例。You will have to call Marshal.ReleaseComObject for the instance of the document, to be able to release it.
e.g.
Marshal.ReleaseComObject(doc);
You will also need to release the instance of
Word.Application
to free the word application instance from the memory.您可以在处理完之后尝试将对象doc设置为null吗?
Can you try to set the object doc to null after you have finished processing it?
你确定这不是后台打印?如果是这样,您可以让它同步打印。省略打印是否会在关闭时解锁文件?
You sure it's not background printing? If so, you could tell it to print syncronously. Does omitting the printing unlock the file on close?