打印 Word 文档时,页边距超出了打印范围

6
我有一段代码,其中我正在打印Word文档。 在示例文档中,有一个部分包含用户修改过的图片边距。
当我执行代码时,我收到以下消息:
“第1节的边距设置在可打印区域之外。”
处理文档后,它开始排队,并抛出此提示enter image description here。如何关闭通知对话框?
我的代码如下:
        Process printJob = new Process();
        printJob.StartInfo.Verb = "PrintTo";
        printJob.StartInfo.Arguments = printerName;
        printJob.StartInfo.ErrorDialog = false;
        printJob.StartInfo.CreateNoWindow = true;
        printJob.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        printJob.StartInfo.FileName = path;
        printJob.StartInfo.UseShellExecute = true;
        printJob.StartInfo.Verb = "print";
        printJob.Start();

这里的变量path >是文件名路径

1个回答

8

http://word.mvps.org/faqs/macrosvba/OutsidePrintableArea.htm

根据此,您需要关闭背景打印,然后关闭Application.DisplayAlerts。
编辑
您无法使用Process完成此操作。 "print"动词使用/x /dde告诉Word打印:
/x从操作shell启动Word的新实例(例如,在Word中打印)。 Word的此实例仅响应一个DDE请求,并忽略所有其他DDE请求和多个实例。 如果您要在操作环境(例如Windows)中启动Word的新实例,则建议您使用/w开关,该开关启动完全功能的实例。
为了抑制消息,您需要使用interop代替:
1.添加对Microsoft.Office.Interop.Word的引用 2.创建Print(string path)方法:
Application wordApp = new Application();
wordApp.Visible = false;

//object missing = Type.Missing;

wordApp.Documents.Open(path); //for VS 2008 and earlier - just give missing for all the args

wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApp.ActiveDocument.PrintOut(false); //as before - missing for remaining args, if using VS 2008 and earlier

wordApp.Quit(WdSaveOptions.wdDoNotSaveChanges); //ditto

我不认为这会对我有所帮助,因为我没有应用程序,只有控制台。 - cpoDesign
查看我的编辑。当您的要求非常简单时,流程打印是不错的选择。 - sq33G
那就是我一直缺少的。你知道如何强制它打印双面吗? - cpoDesign
(wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; 仍然弹出提示框。) - cederlof
(wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;) 对我也行不通。仍然会弹出对话框并且破坏我的代码。 - Deepfreezed
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接