vba 使用 Excel PrintOut 方法时如何防止打印对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/67219/
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
How do you prevent printing dialog when using Excel PrintOut method
提问by James Roes
When I use the PrintOut method to print a Worksheet object to a printer, the "Printing" dialog (showing filename, destination printer, pages printed and a Cancel button) is displayed even though I have set DisplayAlerts = False. The code below works in an Excel macro but the same thing happens if I use this code in a VB or VB.Net application (with the reference changes required to use the Excel object).
当我使用 PrintOut 方法将 Worksheet 对象打印到打印机时,即使我已设置 DisplayAlerts = False,也会显示“打印”对话框(显示文件名、目标打印机、打印的页面和取消按钮)。下面的代码在 Excel 宏中工作,但如果我在 VB 或 VB.Net 应用程序中使用此代码(使用 Excel 对象所需的引用更改),则会发生同样的事情。
Public Sub TestPrint()
Dim vSheet As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set vSheet = ActiveSheet
vSheet.PrintOut Preview:=False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
EDIT: The answer below sheds more light on this (that it may be a Windows dialog and not an Excel dialog) but does not answer my question. Does anyone know how to prevent it from being displayed?
编辑:下面的答案更清楚地说明了这一点(它可能是 Windows 对话框而不是 Excel 对话框),但没有回答我的问题。有谁知道如何防止它被显示?
EDIT: Thank you for your extra research, Kevin. It looks very much like this is what I need. Just not sure I want to blindly accept API code like that. Does anyone else have any knowledge about these API calls and that they're doing what the author purports?
编辑:感谢您的额外研究,凯文。看起来这正是我需要的。只是不确定我想盲目地接受这样的 API 代码。有没有其他人对这些 API 调用有任何了解并且他们正在做作者声称的事情?
采纳答案by Kevin Haines
When you say the "Printing" Dialog, I assume you mean the "Now printing xxx on " dialog rather than standard print dialog (select printer, number of copies, etc). Taking your example above & trying it out, that is the behaviour I saw - "Now printing..." was displayed briefly & then auto-closed.
当您说“打印”对话框时,我假设您的意思是“现在在 xxx 上打印”对话框而不是标准打印对话框(选择打印机、份数等)。以上面的示例并尝试一下,这就是我看到的行为 - “正在打印...”短暂显示然后自动关闭。
What you're trying to control may not be tied to Excel, but instead be Windows-level behaviour. If it is controllable, you'd need to a) disable it, b) perform your print, c) re-enable. If your code fails, there is a risk this is not re-enabled for other applications.
您尝试控制的内容可能与 Excel 无关,而是 Windows 级别的行为。如果它是可控的,您需要 a) 禁用它,b) 执行打印,c) 重新启用。如果您的代码失败,则存在无法为其他应用程序重新启用的风险。
EDIT: Try this solution: How do you prevent printing dialog when using Excel PrintOut method. It seems to describe exactly what you are after.
编辑:试试这个解决方案:How do you prevent Printing dialog when using Excel PrintOut method。它似乎准确地描述了你所追求的东西。
回答by Raghbir Singh
If you don't want to show the print dialogue, then simply make a macro test as follows; it won't show any print dialogue and will detect the default printer and immediately print.
如果您不想显示打印对话框,则只需按如下方式进行宏测试即可;它不会显示任何打印对话框,并将检测默认打印机并立即打印。
sub test()
activesheet.printout preview:= false
end sub
Run this macro and it will print the currently active sheet without displaying the print dialogue.
运行这个宏,它将打印当前活动的工作表而不显示打印对话框。
回答by Robert S.
The API calls in the article linked by Kevin Haines hide the Printing dialog like so:
Kevin Haines 链接的文章中的 API 调用隐藏了打印对话框,如下所示:
- Get the handle of the Printing dialog window.
- Send a message to the window to tell it not to redraw
- Invalidate the window, which forces a redraw that never happens
- Tell Windows to repaint the window, which causes it to disappear.
- 获取打印对话框窗口的句柄。
- 向窗口发送消息告诉它不要重绘
- 使窗口无效,这会强制重绘永远不会发生
- 告诉 Windows 重新绘制窗口,这会导致它消失。
That's oversimplified to put it mildly.
说得委婉一点,那就太简单了。
The API calls are safe, but you will probably want to make sure that screen updating for the Printing dialog is set to True if your application fails.
API 调用是安全的,但如果您的应用程序失败,您可能希望确保将打印对话框的屏幕更新设置为 True。