简体   繁体   English

如何使用vba在PrintPreview中启用“打印”按钮

[英]How to enable Print button in PrintPreview using vba

I'm using below code in Workbook_Open() event: 我在Workbook_Open()事件中使用下面的代码:

Application.Dialogs(xlDialogPrintPreview).Show

at very first time when I click on enable content then preview page is opened but Print option is disabled 在我第一次点击启用内容时,会打开预览页面但禁用打印选项

在此输入图像描述

That is because you are using the Legacy version of print preview. 那是因为您正在使用Legacy版本的打印预览。

Similar question was asked on the official Microsoft forums here 这里官方微软论坛上提出了类似的问题

The best reply quoted here: 这里引用的答案最好:

Hi 你好

By deault the option for settiing print area, rows to repeat and columns to repeat are greyed out if you display the Page Setup dialog via the Print option. 如果通过“打印”选项显示“页面设置”对话框,则通过隐藏设置打印区域的选项,要重复的行和要重复的列将显示为灰色。 "I go to print and go to Page Setup". “我去打印并转到页面设置”。

I would suggest using the Print Titles button on the Page Layout tab in the ribbon to make the neessary changes. 我建议使用功能区中“页面布局”选项卡上的“打印标题”按钮进行必要的更改。

As you are a veteran of previous versions this greying out was the case if you activated Page Setup from the Print Preview options but not through File, Page Setup. 由于您是以前版本的资深人士,如果您从“打印预览”选项激活“页面设置”但不通过“文件”,“页面设置”激活页面设置,则会出现这种情况。 In the shuffling of the user interface from 2007 onwards, File, Print is effectively the equivalent of the old Print Preview and the Page Setup options in the Page Layout tab the equivalent of the "old" File Page Setup. 在2007年以后的用户界面改组中, 文件,打印实际上相当于旧的“打印预览”和“页面布局”选项卡中的“页面设置”选项,相当于“旧”文件页面设置。

Hope this helps 希望这可以帮助

So to sum it up 总结一下

I'd probably recommend the following code instead: Obviously may need some tweaking depending on your desired functionality. 我可能会推荐以下代码:显然可能需要一些调整,具体取决于您所需的功能。 It's not clear what exactly you're trying to achieve. 目前尚不清楚你究竟想要实现什么目标。

Private Sub to_pagelayout()
   ActiveWidow.View = xlPageLayoutView
End Sub

Produces the following result 产生以下结果

在此输入图像描述

As for switching actual ribbon tabs, I think that is best reserved for a separate question. 至于切换实际的功能区标签,我认为最好保留一个单独的问题。 I'll also leave the link to the official documentation here 我还会在这里留下官方文档的链接

You can alternatively use 你也可以使用

Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")

which is the same as pressing File › Print. 这与按File> Print相同。


Since it seems that this odd behavior only occurs when macros were disabled before: 因为看起来这种奇怪的行为只发生在以前禁用宏时:

Define "trusted locations" or digitally sign your VBA code with a trusted certificate to solve this issue. 使用可信证书定义“受信任位置”或对您的VBA代码进行数字签名以解决此问题。 Digitally signed code or code in trusted locations is executed without any message (with the accordingly set properties in the Trust Center). 在受信任位置执行数字签名的代码或代码,无需任何消息(在信任中心中具有相应的设置属性)。

It's anyway a good idea to sign your own/company code to get rid of these messages for trusted code and make people sensitive to that message again (so that it is not clicked away carelessly). 无论如何,最好签署自己的/公司代码,以便为可信代码删除这些消息,并让人们再次对该消息敏感(这样就不会随意点击它)。

Same strange behavior on Excel 2007. Excel 2007上的相同奇怪行为。

Workaround: use Application.OnTime 解决方法:使用Application.OnTime

Private Sub Workbook_Open()

  Application.OnTime Now + TimeValue("00:00:00"), "PrintPreviewOnWorkbookOpen"

End Sub

And the Sub PrintPreviewOnWorkbookOpen in a module Sub PrintPreviewOnWorkbookOpen在模块中打开

Public Sub PrintPreviewOnWorkbookOpen()

  ThisWorkbook.PrintPreview

End Sub

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

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