+ Reply to Thread
Results 1 to 7 of 7

Dynamically Adjust Print Size with VBA Based on Print Zoom %?

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2019
    Location
    United States of America
    MS-Off Ver
    2013
    Posts
    3

    Dynamically Adjust Print Size with VBA Based on Print Zoom %?

    I use an excel worksheet as a template. Information is imported into the template and when I am done I will print a pdf copy via VBA. I then reset the template, import different information and repeat the process. This goes on many times per day. I set up my template to print 1 page wide by 1 page tall as the default. The problem is that sometimes there are too many rows in the template to fit comfortably on one page and the printout is too small to read. If I set my template to print 1 page wide as the default then sometimes a page that is just a little too big will print on two pages when it could have comfortably fit on one.

    Is there a way in VBA to check if printing the template 1 page wide by 1 page tall will zoom below 85% and if so, then print to 1 page wide only?

    Please provide all code as I am new to VBA but learning fast! Thank you!

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Dynamically Adjust Print Size with VBA Based on Print Zoom %?

    You can set your system to automatically do a print preview before printing which would allow you to see what the printed output would look like. You could then stop the print process and make adjustments to the settings before printing, if necessary.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Registered User
    Join Date
    04-21-2019
    Location
    United States of America
    MS-Off Ver
    2013
    Posts
    3

    Re: Dynamically Adjust Print Size with VBA Based on Print Zoom %?

    Thanks for the quick reply JLGWhiz. I have a "Publish" button that I press which saves the template to pdf and attaches it to an email. A print preview also opens up to double check everything before I hit send on my email. I was hoping to automate the sizing in VBA (per my original post) so I don't have to "Easter egg" around with the print sizing (see what I did there being Easter and all). Would you or someone else know how to do this in VBA?

    Thanks again for your response. I appreciate it.

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Dynamically Adjust Print Size with VBA Based on Print Zoom %?

    Quote Originally Posted by FSUDAL View Post
    Thanks for the quick reply JLGWhiz. I have a "Publish" button that I press which saves the template to pdf and attaches it to an email. A print preview also opens up to double check everything before I hit send on my email. I was hoping to automate the sizing in VBA (per my original post) so I don't have to "Easter egg" around with the print sizing (see what I did there being Easter and all). Would you or someone else know how to do this in VBA?

    Thanks again for your response. I appreciate it.
    Each printer has its own software for sizing the actual print and what you see in the print preview is a feedback from what the printer has done with your data as opposed to what you sent the printer. I have never done it, but you can probably interrogate the zoom ratio in the PageSetUp properties to see if it is less than 85 percent before the PrintOut command. But from what I read on the subject, you might get "False" as a value if you have set the PagesTall and PagesWide to 1.

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Dynamically Adjust Print Size with VBA Based on Print Zoom %?

    Hi there,

    See if the following code does what you need:

    
    
    Option Explicit
    
    
    Sub SetZoom()
    
        Const iMINIMUM_ZOOM As Integer = 85
    
        Dim wks As Worksheet
    
        Set wks = ActiveSheet
    
        With wks.PageSetup
    
            .FitToPagesWide = 1
            .FitToPagesTall = 1
            .Zoom = False
    
            Application.ExecuteExcel4Macro "PAGE.SETUP(,,,,,,,,,,,,{1,#N/A})"
            Application.ExecuteExcel4Macro "PAGE.SETUP(,,,,,,,,,,,,{#N/A,#N/A})"
    
            If .Zoom < iMINIMUM_ZOOM Then
                .Zoom = False
            End If
    
        End With
    
    End Sub
    Acknowledgement to Andy Pope for the Excel4 macro part of the code.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  6. #6
    Registered User
    Join Date
    04-21-2019
    Location
    United States of America
    MS-Off Ver
    2013
    Posts
    3

    Re: Dynamically Adjust Print Size with VBA Based on Print Zoom %?

    Thanks Greg M. I have to admit I am a newbie. I incorporated your code with what I already have (see below). The Option Explicit gave me an error even though I put it at the very top (above Private Sub Publish_Click(). I took it out just to see what would happen.

    When I click the Publish button the pdf will save but the pdf print zoom size does not seem to dynamically adjust. It is always 1 page wide. This is fine for very long pages since it needs to be on more than one page (otherwise it is too small to see) but for ones that are just over I would like to squeeze them into a single pdf page as long as the zoom is not below 85. Would you please take a peek at the code below and let me know what may be the problem? I greatly appreciate your help!


    Private Sub Publish_Click()

    'Perform Spellcheck
    Sheets("Rebalancer").CheckSpelling

    'Create pdf file name and save location
    Dim filenamemacro As String
    Dim outlookapp As Object
    Dim OutlookMessage As Object
    Path = CreateObject("WScript.Shell").specialfolders("Desktop")
    filenamemacro = Path & "\" & Sheets("Rebalancer").Range("A1") & Format$(Date, " mm-dd-yyyy")

    'Determine print page size

    Const iMINIMUM_ZOOM As Integer = 85
    Dim wks As Worksheet
    Set wks = ActiveSheet

    With wks.PageSetup

    .FitToPagesWide = 1
    .FitToPagesTall = 1
    .Zoom = False

    Application.ExecuteExcel4Macro "PAGE.SETUP(,,,,,,,,,,,,{1,#N/A})"
    Application.ExecuteExcel4Macro "PAGE.SETUP(,,,,,,,,,,,,{#N/A,#N/A})"

    If .Zoom < iMINIMUM_ZOOM Then
    .FitToPagesWide = 1
    .FitToPagesTall = False
    .Zoom = False
    End If

    End With

    'Save to pdf

    ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=filenamemacro, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

    End Sub

  7. #7
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Dynamically Adjust Print Size with VBA Based on Print Zoom %?

    did you try this
    If .Zoom < iMINIMUM_ZOOM Then
       .FitToPagesWide = 1
        .FitToPagesTall = False
        .Zoom = iMINIMUM_ZOOM 
    End If
    for this current one
    If .Zoom < iMINIMUM_ZOOM Then
    .FitToPagesWide = 1
    .FitToPagesTall = False
    .Zoom = False
    End If
    Last edited by JLGWhiz; 04-22-2019 at 03:57 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Print to PDF, Unknown Zoom factor
    By rob_h in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-21-2017, 04:28 AM
  2. Adjust a Userforms Zoom based on screen resolution.
    By Baziwan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-15-2014, 10:00 AM
  3. VBA to set print range, open box asking for print qty, print, then redefine print area
    By galvinpaddy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-11-2012, 06:43 PM
  4. [SOLVED] zoom in print preview
    By zazzz in forum Excel General
    Replies: 2
    Last Post: 08-10-2011, 02:38 PM
  5. macro to adjust print margins in print preview mode
    By yeoye in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2009, 06:16 AM
  6. Automatically selecting print area based on Pivot Table size
    By mike703 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-29-2008, 11:17 AM
  7. print view & print, in excel, different size cells
    By buddyorliz in forum Excel General
    Replies: 3
    Last Post: 09-01-2005, 09:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1