我有一段代码循环遍历工作簿中的所有工作表(除第一页外),然后将它们打印为PDF,以将所有列和行仅放在一个页面上。如果我使用F8逐步执行代码,这会起作用,但是如果我只运行代码,就好像它完全忽略了代码的With ActiveSheet.PageSetup部分,每个PDF输出都有两页。
下面是我正在使用的代码(从关于这个问题的公认答案中提取和调整:All columns of excelsheet are not fitted in same page of pdf; while converting using Excel VBA)
Dim ctr
ctr = 2
Do While (ctr <= ActiveWorkbook.Sheets.Count)
On Error Resume Next
ActiveWorkbook.Sheets(ctr).Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.25)
.BottomMargin = Application.InchesToPoints(0.25)
.HeaderMargin = Application.InchesToPoints(0.25)
.FooterMargin = Application.InchesToPoints(0.25)
.Orientation = xlPortrait
.PaperSize = xlPaperLetter
.Zoom = 100
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Application.PrintCommunication = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ActiveWorkbook.Path & "\" & ActiveWorkbook.ActiveSheet.Name & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ctr = ctr + 1
Loop
我希望无论我是使用F5运行代码还是使用F8逐步执行代码,都将以相同的方式运行,但在这里似乎不是这种情况,我也不知道为什么。< / p>
更新:下面Rory回答了问题,第二个“ Application.PrintCommunication = False”应该是“ Application.PrintCommunication = True”。
答案 0 :(得分:0)
将第二个master
更改为Application.PrintCommunication = False