为什么在我对文档调用 Close() 后 Word 不释放我的文件?

发布于 2024-08-11 16:25:46 字数 1435 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟若柳尘 2024-08-18 16:25:46

您必须调用 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.

忆梦 2024-08-18 16:25:46

您可以在处理完之后尝试将对象doc设置为null吗?

Can you try to set the object doc to null after you have finished processing it?

浅语花开 2024-08-18 16:25:46

你确定这不是后台打印?如果是这样,您可以让它同步打印。省略打印是否会在关闭时解锁文件?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文