以下子应该为PDF输出准备页面设置。例如,如果由于其他连接的打印机,页面制动器搞砸了,则子应该将其修复为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
当我在'With ws.PateSetup'中添加一个断点时,sub实际上按照预期的单步(F8)工作。但是,如果我使用F5运行它,它会忽略这些语句。调试打印显示,属性的值没有改变。
到目前为止尝试的事情:在.zoom和.FitPagesWide之前添加延迟,使用DoEvents最多1秒。没变。例如,缩放仍然是55.在单步中,Zoom最后读取FALSE。任何解释/暗示这里出了什么问题?
.PrintCommunication
可能是关键。此时的文档相当模糊,但是当.PrintCommunication
为OFF时,它看起来像Excel缓存所有命令,当你打开.PrintCommunication
时将它们转储到页面设置引擎。这可能是在使用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
我也很想看到结果:)