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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1371107/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:45:07  来源:igfitidea点击:

Difference between print and print preview events in excel vba

excelvbaexcel-vbaprintingprint-preview

提问by a_m0d

I have some code which intercepts the Before_Printevent 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.

我有一些代码可以拦截Before_Printexcel 中的事件,以确保用户在打印工作表之前填写了所有必填字段。但是,我只希望在用户实际打印时触发此代码,而不是在他们只是调用打印预览时触发。

Is there any way to tell in the Before_Printcode 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

回答by Robert Mearns

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

回答by guitarthrower

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

回答by Matt Ridge

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:=数量,请将其更改为您想要的任何数字希望...

Enjoy :)

享受 :)