【发布时间】:2010-10-27 01:42:28
【问题描述】:
我正在尝试从我的 C# 代码中打印一个 word 文档。我使用了 12.0.0.0 Word Interop,我想做的是在文档打印之前弹出一个打印对话框。我不能 100% 确定所有这些的语法,因为我无法让我的代码工作:( 有什么想法吗?
提前致谢!
【问题讨论】:
标签: c# printing interop ms-word
我正在尝试从我的 C# 代码中打印一个 word 文档。我使用了 12.0.0.0 Word Interop,我想做的是在文档打印之前弹出一个打印对话框。我不能 100% 确定所有这些的语法,因为我无法让我的代码工作:( 有什么想法吗?
提前致谢!
【问题讨论】:
标签: c# printing interop ms-word
应该是这样的:
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);
}
【讨论】:
接受的答案对我不起作用,所以我找到了另一种方法。这将在后台打印位于c:\temp.docx 的文档,使 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;
【讨论】: