【问题标题】:C#: print out Word document without saving it in advance document.PrintOut()C#:打印出Word文档而不预先保存document.PrintOut()
【发布时间】: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);现在它起作用了。所以原因是在打印作业完成之前关闭了文档。暂停确保有足够的时间完成文档打印作业。感谢您的支持。

标签: c# ms-word document


【解决方案1】:

如果问题是在打印作业完成之前文档正在关闭,那么最好的方法是关闭后台打印,至少在代码执行期间关闭。

winword.Options.PrintBackground = false;

引入后台打印是为了让用户在处理打印作业时继续工作。这对用户来说很好,但对于问题中的代码来说是个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多