C# 使用 Word Interop 和打印对话框进行打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/878302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing using Word Interop with Print Dialog
提问by yeahumok
I'm trying to print a word doc from my C# code. I used the 12.0.0.0 Word Interop and what i'm trying to do is to get a Print Dialogue pop up before the document prints. I'm not 100% sure of the syntax of all of this as I can't get my code to work :( Any ideas?
我正在尝试从我的 C# 代码中打印一个 word 文档。我使用了 12.0.0.0 Word Interop,我想要做的是在文档打印之前弹出打印对话。我不是 100% 确定所有这些的语法,因为我无法让我的代码工作:( 有什么想法吗?
Thanks in advance!
提前致谢!
采纳答案by McAden
It should be something along the lines of:
它应该是这样的:
object nullobj = Missing.Value;
doc = wordApp.Documents.Open(ref file,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
doc.Activate();
doc.Visible = true;
int dialogResult = wordApp.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint].Show(ref nullobj);
if (dialogResult == 1)
{
doc.PrintOut(ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj);
}
回答by Trev
The accepted answer didn't work for me, so I found another way. This will print a document at c:\temp.docx
in the background, keeping Word hidden from view.
接受的答案对我不起作用,所以我找到了另一种方法。这将c:\temp.docx
在后台打印文档,使 Word 隐藏在视图中。
It uses Microsoft.Office.Interop.Word
.
它使用Microsoft.Office.Interop.Word
.
Word.Application wordApp = new Word.Application();
wordApp.Visible = false;
PrintDialog pDialog = new PrintDialog();
if (pDialog.ShowDialog() == DialogResult.OK)
{
Word.Document doc = wordApp.Documents.Add(@"c:\temp.docx");
wordApp.ActivePrinter = pDialog.PrinterSettings.PrinterName;
wordApp.ActiveDocument.PrintOut(); //this will also work: doc.PrintOut();
doc.Close(SaveChanges: false);
doc = null;
}
// <EDIT to include Jason's suggestion>
((Word._Application)wordApp).Quit(SaveChanges: false);
// </EDIT>
// Original: wordApp.Quit(SaveChanges: false);
wordApp = null;