简体   繁体   English

Excel VBA:获取与FitToPageWide对应的缩放级别

[英]Excel VBA: getting zoom level corresponding with FitToPageWide

I am trying to build a macro in Excel which loops through all worksheets, and based on the largest sheet, sets the zoom level to the same level for all worksheets so they all fit on one page but have the same scale (which is needed in printing). 我试图在Excel中构建一个循环遍历所有工作表的宏,并基于最大的工作表,将缩放级别设置为所有工作表的相同级别,以便它们都适合一个页面但具有相同的比例(这是需要的打印)。

I am however having trouble with determining the zoom level which makes sure the biggest page fits to a 1 page width. 但我确定缩放级别时遇到问题,确保最大页面适合1页宽度。

When setting a worksheets width to fit on one page by using .PageSetup.FitToPagesWide = 1 the .PageSetup.Zoom property automatically gets set to FALSE. 通过使用.PageSetup.FitToPagesWide = 1将工作表宽度设置为适合一页时, .PageSetup.Zoom属性自动设置为FALSE。

Setting the FitToPage properties back to false, the zoom level is unchanged from what it was before fitting to one page. 将FitToPage属性设置为false,缩放级别与适合一个页面之前的级别保持不变。

When manually setting the sheet so it fits to one page wide, Excel does show which zoom level corresponds to this, but it seems there is no way to read this in VBA. 当手动设置工作表使其适合一页宽度时,Excel确实显示哪个缩放级别与此对应,但似乎无法在VBA中读取此内容。 Could someone help me with this issue? 有人可以帮我解决这个问题吗?

This post is getting rather old, but as I've been sitting with a similar problem, this question gave me a possible answer. 这篇文章变得相当陈旧,但由于我一直坐在一个类似的问题,这个问题给了我一个可能的答案。

Using a slightly redone code posted by Tom Urtis ( https://www.mrexcel.com/forum/excel-questions/67080-page-setup-zoom-property.html ) the following code extract the zoom iteratively, and then sets the zoom of all pages. 使用Tom Urtis发布的略微重做的代码( https://www.mrexcel.com/forum/excel-questions/67080-page-setup-zoom-property.html ),以下代码迭代地提取缩放,然后设置缩放所有页面。

Option Explicit
#If Win64 Then
    Declare PtrSafe Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#Else
    Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#End If
Sub SetSameZoomOnAllWorksheets()
    On Error GoTo failed
    Dim initial_sheet As Worksheet, Sheet As Worksheet, minzoom As Double
    With Application
        'stuff to speed up the process and avoid any visible changes by the user
        .ScreenUpdating = False
        .DisplayStatusBar = False
        .EnableEvents = False
        '.Visible = false 'Uncomment on a really slow document to make people freak out. Make sure to have the on error so that you'll set it to visble again
        ActiveSheet.DisplayPageBreaks = False
    End With
    Set initial_sheet = ThisWorkbook.Worksheets(ActiveSheet.name)
    minzoom = 400 ' max value set by zoom
    'iterate over each sheet
    For Each Sheet In ThisWorkbook.Worksheets
        minzoom = Application.Min(minzoom, GetOnePageZoom(Sheet))
    Next Sheet
    'iterate over each sheet once more and set the zoom to the lowest zoom
    For Each Sheet In ThisWorkbook.Worksheets
        With Sheet
            If .Visible = xlSheetVisible Then
                .Select
                .PageSetup.Zoom = minzoom
            End If
        End With
    Next Sheet
    initial_sheet.Select
failed:
    With Application
        'Change it back so that the user may see any changes, perform calculations and so on
        .ScreenUpdating = True
        .DisplayStatusBar = True
        .EnableEvents = True
        ActiveSheet.DisplayPageBreaks = True
        '.Visible = True 'This one is very important to unmark if you have marked .visible = false at the top
    End With
End Sub
Function GetOnePageZoom(ByRef Sheet As Worksheet) As Double
    With Sheet
        If .Visible = xlSheetVisible Then
            .Select
            'LockWindowUpdate locks the specified window for drawing - https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-lockwindowupdate
            'XLMAIN is the current active window in excel
            LockWindowUpdate FindWindowA("XLMAIN", Application.Caption)
            .PageSetup.FitToPagesWide = 1
            .PageSetup.Zoom = False
            'pre-send keys for next command to specify: On pagesetup Dialog Press P to open the 'Print', then press alt + A to set page setup to adjust (Automatically moves into the zoom field but keeps the value), press enter
            'This changes the pagesetup from 'fitstopageswide = 1' to 'automatic' but keeps the zoom at whatever level it was set to by the fitstopageswide
            SendKeys "P%A~"
            Application.Dialogs(xlDialogPageSetup).Show
            LockWindowUpdate 0
            GetOnePageZoom = .PageSetup.Zoom
            Debug.Print .PageSetup.Zoom
        Else
            GetOnePageZoom = 400
        End If
    End With
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM