-->

VBA语句PAGESETUP仅在调试模式下执行(VBA statements for pageset

2019-09-30 00:26发布

下面的子应该对PDF输出准备PAGESETUP。 例如,如果由于其他连接的打印机的pagebrakes都搞砸了,子应该修复它回到1页宽和3页高。

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

子实际工作预期在单步(F8),当我添加断点“随着ws.PateSetup”。 如果我运行使用F5它,但是,它忽略了声明。 调试打印表明,对于属性的值并没有改变。

事情试过到目前为止:添加.zoom和.FitPagesWide与之前的DoEvents延迟长达1秒。 没变。 放大的实例保持55.单步,放大读到底FALSE。 任何说明/提示什么错吗?

Answer 1:

.PrintCommunication可能是关键所在。 该文档是相当模糊的在这一点上,但它看起来像Excel中缓存所有的命令时.PrintCommunication关闭并转储页面设置引擎,当你打开.PrintCommunication ON。 这可能是与F5运行时,没有看到任何改变的原因。 (调试的服务更加晦涩我。)

尝试应用

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

我'也好奇,想看看结果:)



文章来源: VBA statements for pagesetup only executed in debug mode