我正在尝试从我的C#代码中打印Word文档。我使用了12.0.0.0 Word Interop,我希望在文档打印之前弹出“打印对话框”。由于无法让代码正常工作,我对所有这些语法不是100%确定。
有什么建议吗?
谢谢!
谢谢!
它应该是类似于以下内容:
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;