通过 Word Interop 打印的文档立即从打印队列中消失 [英] Document printed via Word Interop immediately disappears from print queue

查看:62
本文介绍了通过 Word Interop 打印的文档立即从打印队列中消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# WinForm 应用程序,它通过在书签处放置文本来打开并填写 MS Word dotx 模板,然后尝试打印它,全部使用 MS Word Interop 15.

I have a C# WinForm application that opens and fills out a MS Word dotx template by placing text at bookmarks, and then attempts to print it, all using MS Word Interop 15.

似乎一切正常,打印对话框显示并完成,打印作业显示在打印队列中(即 MS Windows 10 上设备和打印机"中的查看正在打印的内容"窗口).但是,该作业在可以假脱机之前立即从队列中消失!(文档以假脱机"状态出现非常非常短暂,并且不打印 - 打印机永远不会得到作业)

Everything seems to go fine, the print dialog shows and completes OK, the print job shows up in the print queue (i.e. the "See what's printing" window from "Devices and Printers" on MS Windows 10). But then the job immediately disappears from the queue before it can be spooled! (document appears very very briefly with "Spooling" status, and does not print - the printer never gets the job)

这是我的代码(为简洁起见删除了异常检查):

Here is my code (exception checking removed for brevity):

using Word = Microsoft.Office.Interop.Word;
private void Print_Click(object sender, EventArgs e)
{
    // Open the MS Word application via Office Interop
    Word.Application wordApp = new Word.Application();
    Word.Document wordDoc;
    // Open the template
    wordDoc = wordApp.Documents.Add(Template: ContractTemplatePath, Visible: false);
    // Ensure the opened document is the currently active one
    wordDoc.Activate();

    // Set the text for each bookmark from the corresponding data in the GUI
    SetBookmarkText(wordDoc, "Foo", fooTextBox.Text);
    // ... There's a whole bunch of these ... then:

    // Instantiate and configure the PrintDialog
    var pd = new PrintDialog()
    {
        UseEXDialog = true,
        AllowSomePages = false,
        AllowSelection = false,
        AllowCurrentPage = false,
        AllowPrintToFile = false
    };

    // Check the response from the PrintDialog
    if (pd.ShowDialog(this) == DialogResult.OK)
    {
        // Print the document
        wordApp.ActivePrinter = pd.PrinterSettings.PrinterName;
        wordDoc.PrintOut(Copies: pd.PrinterSettings.Copies);
    }

    // Close the document without saving the changes (once the 
    // document is printed we don't need it anymore). Then close 
    // the MS Word application.
    wordDoc.Close(SaveChanges: false);
    wordApp.Quit(SaveChanges: false);
}

我在这里唯一能想到的是,可能是因为我将文档发送到打印机后立即将其删除,然后作业还没有完全发送,因此它会自行删除或其他内容.如果,那么我如何确定我需要将文档保留多长时间以及等待它的最佳方法是什么?

The only thing I can think of here is that maybe because I do away with the document as soon as I've sent it to the printer, then the job hasn't been completely sent so it removes itself or something. If this is the case then how can I determine how long I need to keep the document around for and what's the best way of waiting for that?

我又做了一点研究(目前没有时间对此进行更多研究)表明我可以使用 PrintEnd 事件,但我不能立即看看这在使用 Interop 时是否适用.这是一种无需轮询即可实现我想要的方法吗?

Ive done another small bit of research (dont have time for more on this just at this moment) that suggests I may be able to use the PrintEnd event, but I couldn't immediately see if this would be applicable when using Interop. Would it be a method of achieving what I want without polling?

推荐答案

一种解决方案是轮询 BackgroundPrintingStatus 属性.它包含仍在打印队列中等待的文档计数.虽然此计数大于 0,但仍有文档等待打印.

One solution is to poll the BackgroundPrintingStatus property of the Word application. It holds a count of the documents still waiting in the printing queue. While this count is greater than 0 there are still documents awaiting printing.

您可以通过多种方式实现这一目标.这是一个阻塞 UI 的简单循环:

There are many ways you could achieve this. Here's a simple loop which blocks the UI:

// Send document to printing queue here...

while (wordApp.BackgroundPrintingStatus > 0)
{
    // Thread.Sleep(500);
}

// Printing finished, continue with logic

或者,您可能希望将其包装在一个任务中,以便您可以在等待时做其他事情:

Alternatively you may want to wrap it in a task so that you can do other things while waiting:

await Task.Run(async () => { while (wordApp.BackgroundPrintingStatus > 0) 
                                   { await Task.Delay(500); } });

这篇关于通过 Word Interop 打印的文档立即从打印队列中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆