excel vba中打印和打印预览事件的区别
我有一些代码可以拦截 Excel 中的 Before_Print
事件,以确保用户在打印工作表之前已填写所有必填字段。但是,我只希望在用户实际打印时触发此代码,而不是在他们只是调用打印预览时触发。
有什么方法可以在 Before_Print
代码中判断用户是在实际打印还是只是在预览?
我当前拥有的代码是(事件存根由excel生成):
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Not Sheet2.CheckAllFieldsFilled Then
Cancel = True
End If
End Sub
I have some code which intercepts the Before_Print
event in excel to make sure that the user has filled in all the required fields before they print the sheet. However, I only want this code to fire when the user is actually printing, not when they are just calling the print preview.
Is there any way to tell in the Before_Print
code whether the user is actually printing or just previewing?
The code that I currently have is (event stub was generated by excel):
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Not Sheet2.CheckAllFieldsFilled Then
Cancel = True
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为没有一种巧妙的方法来确定该事件是打印预览还是打印请求。
下面的解决方案不是特别简洁,并且给用户带来了轻微的不便,但它确实有效。
该代码取消该事件,然后提示用户,根据用户的响应显示打印预览或打印。
I don't think there is a neat way to determine if the event is a print preview or print request.
The solution below is not particularly neat and inconveniences the user slightly, but it works.
The code cancels the event and then prompts the user, based on their response it displays the print preview or prints.
我想我会提供一个非常明显的按钮,供用户在想要打印预览时按下。
使按钮隐藏以进行打印(在按钮的选项中),并让代码简单地说:
I think I would provide a very visible button for the user to push when wanting to Print Preview.
Make the button hide for printing (in the options for the button), and have the code simply say:
要打印,您可以执行以下操作:
按照建议进行打印预览:
每个按钮都需要不同的按钮,但无论哪种方式,我都强烈建议测试您是否确实需要两者,因为预览按钮可能适用于您的打印选项,特别是因为在大多数情况下您可以直接从预览进行打印。
我在这里可能是错的,但我不这么认为。
请注意,我在这里发布的打印选项将直接打印,它不会请求选项,因为它们已被编码到脚本中,如果您想更改要打印的份数,请更改
copies :=
到您想要的任何数字...享受吧:)
To print you could do something like this:
To Print Preview as was suggested:
Each one would would require a different button, but either way I would strongly suggest testing if you really need both, because the preview button may work for your print option, especially since you would in most cases be able to print straight from the preview.
I may be wrong here, but I don't think so.
Just a heads up, the print option I posted here will directly print, it won't request options, because they have been coded into the script, if you want to change how many copies you wish to print, change the
copies:=
to whatever number you wish...Enjoy :)