简体   繁体   中英

Excel Worksheet Saved As PDF - Print Area Changes

I have an excel worksheet and I have a macro that saves the worksheet as a PDF File to a directory on our network. When I view the Print Preview in Excel it is on 1 sheet. However when I open the PDF File it is bleeding onto a second page. It is formatting in landscape, and in the PDF there is a bit of 1 table and 1 graph showing the on the left side of the second sheet (the Right side of the page bleeds over to the next page).

I've tried so many things to get this to stick but it won't work. Here are some of the things I've tried:

  • Manually setting the Print Area and Page Breaks
  • Updated the Print Area and Page Breaks after the data updates via a macro.
  • Print settings to 1 page by 1 page (manually in via the macro)
  • Set print dpi (manually and via the macro)

Any insight would be great. Thanks in advance.

I had the same problem to export a range of sheets (with only print areas exported) and I solved it. It was a syntax problem. Before ExportAsFixedFormat don't put "Selection" but "Activesheet" instead, with the Activesheet one of the exported sheets or not.

ActiveWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select

Sheets("Sheet1").Activate

Activesheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"D:\myPath\myFileName " & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=showAfterSave 

Try...

Sub Macro1()
ActiveWorkbook.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="Example", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
From:=1, _
To:=1, _
OpenAfterPublish:=True
End Sub

I know it seems stupid but i found that doing it twice solves problem :)

Sub Main(saveToPdf as boolean)

If saveToPdf Then

'save to pdf
    ActiveSheet.Range(ActiveSheet.PageSetup.PrintArea).Select
    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "D:\myPath\myFileName " & year & ".pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

    Call oneMoreTimeBecauseItSolvesProblem(year, showAfterSave)
End If 
End Sub

Sub oneMoreTimeBecauseItSolvesProblem(year As Integer, showAfterSave As Boolean)

    On Error Resume Next
    ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
    Application.ScreenUpdating = True

'save to pdf
    ActiveSheet.Range(ActiveSheet.PageSetup.PrintArea).Select
    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "D:\myPath\myFileName " & year & ".pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=showAfterSave 
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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