页面设置的VBA语句仅在调试模式下执行 [英] VBA statements for pagesetup only executed in debug mode
问题描述
下面的子程序应该为PDF输出准备页面设置. 例如,如果由于其他连接的打印机而使页面制动器弄乱了,则子机应将其固定回1页宽和3页高.
The following sub is supposed to prepare pagesetup for a PDF output. For instance, if due to other attached printers the pagebrakes are messed up, the sub should fix it back to 1 page wide and 3 pages tall.
Sub adjustPB(ws As Variant, ps As XlPaperSize)
'On Error Resume Next
Application.DisplayAlerts = False
Application.PrintCommunication = False
With ws.PageSetup
.LeftMargin = Application.InchesToPoints(0)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0)
.BottomMargin = Application.InchesToPoints(0)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
.Orientation = xlLandscape
'.Orientation = xlPortrait
.PaperSize = ps
.Zoom = 100
.Zoom = False
Debug.Print .Zoom
.FitToPagesWide = 1
Debug.Print .Zoom
Debug.Print .FitToPagesWide
.FitToPagesTall = 3
End With
Application.DisplayAlerts = True
Application.PrintCommunication = True
End Sub
当我在"With ws.PateSetup"处添加断点时,该子实际上可以按单个步骤(F8)的预期工作. 但是,如果我使用F5运行它,它将忽略这些语句. 调试打印显示,这些属性的值未更改.
The sub actually works as expected in single step (F8), when I add a breakpoint at 'With ws.PateSetup'. If I run it using F5, however, it ignores the statements. The debug prints show, that the values for the properties didn't change.
到目前为止尝试过的事情: 使用DoEvents在.zoom和.FitPagesWide之前添加延迟,最多1秒钟.没变化.缩放比例仍然为55. 单步执行后,Zoom最终读取FALSE. 有任何解释/提示出什么事了吗?
Things tried so far: add delays before .zoom and .FitPagesWide with DoEvents for up to 1 sec. No change. Zoom for instance remains 55. In singlestep, Zoom reads FALSE in the end. Any explanations / hints what's going wrong here?
推荐答案
.PrintCommunication
可能是关键.该文档在这一点上还很模糊,但是看起来当.PrintCommunication
为OFF时,Excel会缓存所有命令,而当您打开.PrintCommunication
为ON时,它们会转储到页面设置引擎中.这可能是使用F5运行时看不到任何更改的原因. (调试器的服务对我来说更晦涩.)
.PrintCommunication
may be the key. The documentation is rather obscure at this point but it looks like Excel caches all commands when .PrintCommunication
is OFF and dumps them to page setup engine when you turn .PrintCommunication
ON. That may be the reason for not seeing any changes when running with F5. (The services of the debugger are even more obscure for me.)
尝试申请
With ActiveSheet.PageSetup
' ....
.Parent.Application.PrintCommunication = True
.Zoom = 100
.Zoom = False
Debug.Print .Zoom
.FitToPagesWide = 1
Debug.Print .Zoom
Debug.Print .FitToPagesWide
.FitToPagesTall = 3
.Parent.Application.PrintCommunication = False
' ....
End With
Application.PrintCommunication = True
我也很好奇看到结果:)
I'am also curious to see the result :)
这篇关于页面设置的VBA语句仅在调试模式下执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!