gpt4 book ai didi

c# - word 自动打印输出 : wait for its finished?

转载 作者:行者123 更新时间:2023-11-30 22:26:40 25 4
gpt4 key购买 nike

我正在使用 word 自动化生成一个 docx 文件,然后将其打印出来。这是我的基本代码。我发现adoc.PrintOut之后要等一段时间,否则可能会打印失败。看来是因为我在那之后关闭了文件。在文件关闭之前,word 还来不及打印出来。所以,基本上,adoc.PrintOut 是非阻塞的(这可能不是正确的术语)。现在,我等了 10 秒钟,效果很好。但是,它对此有更优雅的解决方案吗?我的意思是,如何等到 PrintOut 完成,然后关闭文档?

谢谢

object yes = true;
object no = false;
object missing = System.Reflection.Missing.Value;


Word.Application wordApp = new Word.Application();

object fileName = originalFileName;
Word.Document adoc = null;
try
{
adoc = wordApp.Documents.Open(ref fileName,
ref missing, ref no, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref yes, ref missing, ref missing, ref missing, ref missing);

adoc.Activate();

/*
some other processing
*/



object sFile = fileName;

adoc.PrintOut(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

// http://msdn.microsoft.com/en-us/library/b9f0ke7y
//adoc.PrintPreview();

for (int i = 0; i < 10; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(1));
Application.DoEvents();
}
//adoc.Save();

object savechanges = Word.WdSaveOptions.wdDoNotSaveChanges; //.wdSaveChanges;
object originalFormat = Word.WdOriginalFormat.wdWordDocument;
object routeDocument = missing; // Type.Missing; // true;

((Word._Document)adoc).Close(ref savechanges, ref originalFormat, ref routeDocument);

((Word._Application)wordApp).Quit(ref savechanges, ref missing, ref missing);

adoc = null;
wordApp = null;
}
catch
{

}
finally
{
/*
some dispose work
*/

// do it twice to release all memory
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

}

最佳答案

可以使用PrintOut的第一个参数。

object background = false;
adoc.PrintOut(background, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing);

正如文档所说: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.printout(v=vs.80).aspx

“背景如果在 Microsoft Office Word 打印文档时继续自定义代码,则为 true。”

关于c# - word 自动打印输出 : wait for its finished?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11674925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com