Re: Page.setup Fails Under Worksheet_activate()
Thanks for the clarification, Bill. I am running this code under a sheet module and it does execute OK but produces the wrong result there. My version of this code is actually slightly different from the original post in that the PagesTall value is variable:
- MinPrintZoom = 46
-
- With Application.ActiveSheet.PageSetup
- .PrintTitleRows = "$1:$2"
- .PrintTitleColumns = "$A:$B"
- .LeftMargin = Application.InchesToPoints(0.25)
- .RightMargin = Application.InchesToPoints(0.25)
- .TopMargin = Application.InchesToPoints(0.25)
- .BottomMargin = Application.InchesToPoints(0.25)
- .Zoom = 101 'Set to large default value
- PagesTall = 0
- Do
- PagesTall = PagesTall + 1
- PagesTallZoom = .Zoom 'Capture current zoom value
-
- '...
- Application.ExecuteExcel4Macro "PAGE.SETUP(,,,,,,,,,,,,{1," & PagesTall & "})" '{Wide, Tall}
- Application.ExecuteExcel4Macro "PAGE.SETUP(,,,,,,,,,,,,{#N/A,#N/A})" '{Wide, Tall}
-
- MsgBox "Pages Tall = " & PagesTall & ", Paper Size = " & PaperSize & _
- ", Orientation = " & Orientation & ", Zoom = " & .Zoom 'Debug
- '...
- 'Check of a minimum allowed zoom value has been reached or if no more progress can be made with these settings
-
- Loop Until .Zoom >= MinPrintZoom Or .Zoom = PagesTallZoom
- MsgBox (.Zoom >= MinPrintZoom) & ": Zoom = " & .Zoom 'Debug
- End With
Display More
When I run this manually, it runs fine, returning a value of 51% for the data on the particular page being evaluated. But when I run it under Worksheet_Activate() the PagesTall and PagesWide fail to be set to the specified values, and .zoom always returns 100%, regardless of what data is actually on the sheet.
My sense of it is that the problem lies with the first call to PAGE.SETUP which seems to be failing (though it doesn't return a trappable error condition), so the PagesTall and PagesWide end up set to the default instead. (The resulting state of the sheet confirms this.) So, in a sense, the resulting zoom value of 100% is correct, but only because the Tall/Wide update call failed.
Thanks in advance for your help!
Peter