excel vba中打印和打印预览事件的区别

发布于 2024-08-04 06:33:08 字数 379 浏览 15 评论 0原文

我有一些代码可以拦截 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖伴 2024-08-11 06:33:08

我认为没有一种巧妙的方法来确定该事件是打印预览还是打印请求。

下面的解决方案不是特别简洁,并且给用户带来了轻微的不便,但它确实有效。

该代码取消该事件,然后提示用户,根据用户的响应显示打印预览或打印。

Private Sub Workbook_BeforePrint(Cancel As Boolean)

Dim Print_or_Preview As XlYesNoGuess

Application.EnableEvents = False

    Cancel = True
    Print_or_Preview = MsgBox("Show Print Preview?", vbYesNo)

    If Print_or_Preview = True Then
        ActiveWindow.ActiveSheet.PrintPreview
        Else
        ActiveWindow.ActiveSheet.PrintOut
    End If
Application.EnableEvents = True

End Sub

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.

Private Sub Workbook_BeforePrint(Cancel As Boolean)

Dim Print_or_Preview As XlYesNoGuess

Application.EnableEvents = False

    Cancel = True
    Print_or_Preview = MsgBox("Show Print Preview?", vbYesNo)

    If Print_or_Preview = True Then
        ActiveWindow.ActiveSheet.PrintPreview
        Else
        ActiveWindow.ActiveSheet.PrintOut
    End If
Application.EnableEvents = True

End Sub
萌逼全场 2024-08-11 06:33:08

我想我会提供一个非常明显的按钮,供用户在想要打印预览时按下。

使按钮隐藏以进行打印(在按钮的选项中),并让代码简单地说:

ActiveWindow.ActiveSheet.PrintPreview

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:

ActiveWindow.ActiveSheet.PrintPreview
魔法唧唧 2024-08-11 06:33:08

要打印,您可以执行以下操作:

ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

按照建议进行打印预览:

ActiveWindow.ActiveSheet.PrintPreview 

每个按钮都需要不同的按钮,但无论哪种方式,我都强烈建议测试您是否确实需要两者,因为预览按钮可能适用于您的打印选项,特别是因为在大多数情况下您可以直接从预览进行打印。

我在这里可能是错的,但我不这么认为。

请注意,我在这里发布的打印选项将直接打印,它不会请求选项,因为它们已被编码到脚本中,如果您想更改要打印的份数,请更改 copies := 到您想要的任何数字...

享受吧:)

To print you could do something like this:

ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

To Print Preview as was suggested:

ActiveWindow.ActiveSheet.PrintPreview 

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文