Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:打印Word文档而不事先保存文档。打印输出()_C#_Ms Word_Document - Fatal编程技术网

C#:打印Word文档而不事先保存文档。打印输出()

C#:打印Word文档而不事先保存文档。打印输出(),c#,ms-word,document,C#,Ms Word,Document,我想打印出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.Intero

我想打印出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);

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

winword.Options.PrintBackground = false;

引入后台打印是为了允许用户在处理打印作业时继续工作。这对用户来说没什么问题,但是问题中的代码有问题。

您应该能够打印而不保存。当你尝试时会发生什么?它不会打印。只有在命令document Print.Out()生效之前保存了文档。您在问题中提供的代码对我来说效果很好-新文档打印时注释掉了
SaveAs2
行。请注意,作业到达打印机前需要几秒钟(明显的停顿)。如果您在打印输出后注释掉所有内容,这样代码就不会对Word做任何其他操作,这会改变什么吗?好的,谢谢您的提示。我发现PrintOut命令后面的代码就是原因。我用document.close关闭文档(false,ref缺失,ref缺失);打印完成之前,文档可能已关闭。如果我设置save before close=true,它将打印。我是否能够确保在关闭文档之前完成打印作业?我添加了一个线程。Sleep(1000);现在它起作用了。因此,原因是打印作业完成前文档已关闭。暂停可确保有足够的时间完成文档打印作业。谢谢你的支持。