简体   繁体   English

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

[英]Difference between print and print preview events in excel vba

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.我有一些代码可以拦截 excel 中的Before_Print事件,以确保用户在打印工作表之前填写了所有必填字段。 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?有没有办法在Before_Print代码中判断用户是实际打印还是只是预览?

The code that I currently have is (event stub was generated by excel):我目前拥有的代码是(事件存根由 excel 生成):

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    If Not Sheet2.CheckAllFieldsFilled Then
        Cancel = True
    End If
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

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...请注意,我在这里发布的打印选项将直接打印,它不会请求选项,因为它们已被编码到脚本中,如果您想更改要打印的copies:= ,请将copies:=更改为你想要什么数字...

Enjoy :)享受 :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM