我有一个C#WinForm应用程序,通过将文本放在书签上打开并填充MS Word dotx模板,然后尝试打印它,所有这些都使用MS Word Interop 15.通过Word Interop打印的文档立即从打印队列中消失
一切似乎都很好,打印对话框显示并完成确定,打印作业显示在打印队列中(即MS Windows 10上的“设备和打印机”中的“查看正在打印的内容”窗口)。但是,这个工作立即从队列中消失,然后才能被假脱机! (文档出现非常非常短暂地用“假脱机”状态,并且不打印 - 打印机从来没有得到这份工作)
这里是我的代码(异常检查简洁,删除):
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);
}
唯一我可以想到的是,也许是因为我一旦将文档发送到打印机就不再使用该文档,那么该作业还没有完全发送,因此它会自行删除。如果这是的情况下,那么我怎样才能确定我需要多长时间保持文件,以及等待的最佳方式是什么?
编辑:香港专业教育学院做研究的另一个小一点(不要刚在这一刻时间,更多关于这个),这表明,我可能能够使用PrintEnd事件,但我不能立即看到这是否会在使用Interop时适用。这是否是一种在没有投票的情况下实现我想要的方法?
你可能轮询的数'wordApp.BackgroundPrintingStatus'并等待它为0.我无法测试这个来验证,所以只是一个评论。 – Equalsk
@Equalsk我最终使用了你所建议的方法(尽管我不喜欢轮询......我想我可以在一个单独的线程上完成)。无论如何,如果你想回答相同的效果,我会接受它。 – Toby