excel vba not exporting pagesetup to pdf correctly

后端 未结 3 1949
-上瘾入骨i
-上瘾入骨i 2020-12-11 05:41

I have code which formats a worksheet to the desired setup and layout (one page wide and tall in landscape). When I run the code (part of a long macro) it formats the pagese

相关标签:
3条回答
  • 2020-12-11 05:58

    I have found what seems to be the solution:

    Application.PrintCommunication = False
        With ActiveSheet.PageSetup
            .Orientation = xlLandscape
            .Zoom = False
            '.PrintArea = Worksheets(ReportWsName).UsedRange
            .FitToPagesWide = 1
            '.FitToPagesTall = 1
        End With
    Application.PrintCommunication = True
    

    I needed to add the Application.PrintCommunication part to the equation. For whatever reason Excel would overwrite the settings I was putting if I ran the code without it.

    0 讨论(0)
  • 2020-12-11 06:00

    I think the problem is that you need to add the .Zoom = False to your page setup code:

    'Format Print Area to one Page
    With ActiveSheet.PageSetup
        .PrintArea = Worksheets(ReportWsName).UsedRange
        .Orientation = xlLandscape
        .FitToPagesWide = 1
        .Zoom = False 'I have added this line
    End With
    

    From what I have tried this should solve it for you.

    Let me know how it goes!

    EDIT: Maybe you need:

    'Format Print Area to one Page
    With ActiveSheet.PageSetup
        .PrintArea = Worksheets(ReportWsName).UsedRange
        .Orientation = xlLandscape
        .FitToPagesWide = 1
        .FitToPagesTall = 1
        .Zoom = False 'I have added this line
    End With
    

    EDIT2: What if you changed:

    .PrintArea = Worksheets(ReportWsName).UsedRange

    To

    .PrintArea = Worksheets(ReportWsName).UsedRange.Address

    0 讨论(0)
  • 2020-12-11 06:06

    Yes!!!, I have had the same problem: I was not able to export a sheet with the page Setup settings already applied on it.

    Before trying the Application.PrintCommunication I tested Wait and Sleep commands without success. Finally I skipped this issue by using CopyPicture method, adding a chart page and then exporting it to pdf, but resolution in my pdf it was not fine and I was not able to play with margins.

    So just add Application.PrintCommunication=false before your code , on pagesetup settings like CaptainABC says and most important: close with Application.PrintCommunication=true after the code.

    Thank you for this useful post.

    0 讨论(0)
提交回复
热议问题