【发布时间】:2020-01-19 01:00:04
【问题描述】:
我想打印一个 Word 文档而不预先保存它。这可能吗?
//I created an instance for word app
Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
//I created a Word document (including pararaphs and tables):
Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
//I can print the document, if I save it before. But I want to print it without saving the word document.
document.SaveAs2(@"C:\User\\Desktop\Test");
document.PrintOut()
//Export of the document as pdf-file.
document.ExportAsFixedFormat(label24.Text + "Document" + textBox13.Text, WdExportFormat.wdExportFormatPDF, true);
【问题讨论】:
-
您应该可以在不保存的情况下进行打印。当你尝试时会发生什么?
-
它不打印。仅当在命令文档 Print.Out() 起作用之前已保存文档。
-
您在问题中提供的代码对我来说很好用 - 新文档打印时将
SaveAs2行注释掉。请注意,在作业到达打印机之前需要几秒钟(明显的暂停)。如果您注释掉PrintOut之后的所有内容,以便代码不会尝试对 Word 执行任何其他操作,那会改变什么吗? -
好的,谢谢您的提示。我发现 PrintOut 命令后面的代码是原因。我用 document.Close(false,ref missing,ref missing); 关闭文档可能在打印完成之前文档已关闭。如果我在 close = true 之前设置保存它会打印。我能否在关闭文档之前确保打印作业已完成?
-
我添加了一个 Thread.Sleep(1000);现在它起作用了。所以原因是在打印作业完成之前关闭了文档。暂停确保有足够的时间完成文档打印作业。感谢您的支持。