为何无法横向输出为pdf?

请将附件下载成 sample.xlsx
链接: https://pan.baidu.com/s/1Q_Rcf-9L2jw9rkFKr2rxqw?pwd=mznq 提取码: mznq
很简单的格式
图片.png

我希望保存成pdf,使用下面的代码

Sub pdf()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim saveLocation As String
    templePath = "d:\sample.xlsx"
    Set wb = Workbooks.Open(templePath)
    Set ws = wb.Sheets(1)
    With ws.PageSetup
        .Orientation = xlLandscape
    End With
    saveLocation = "d:\sample.pdf"
    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation
    wb.Close SaveChanges:=False
    Set wb = Nothing
    Set ws = Nothing
End Sub

产生的结果,仅仅显示了一半

图片.png

设置成landscape,没有起作用

With ws.PageSetup
    .Orientation = xlLandscape
End With

请问:如何修改代码?

图片.png

阅读 451
2 个回答

这玩意后端直接导除啊

我添加了 .FitToPagesWide = 1 和 .FitToPagesTall = False,使工作表的宽度适合一页,并且高度可以跨多页。另外设置 .Zoom = False 使页面缩放不会影响布局。
templePath和saveLocation路径要改成你自己的

Sub pdf()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim saveLocation As String
    Dim templePath As String
    
    On Error GoTo ErrorHandler
    
    templePath = "d:\sample.xlsx"
    Set wb = Workbooks.Open(templePath)
    Set ws = wb.Sheets(1)
    
    With ws.PageSetup
        .Orientation = xlLandscape
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .Zoom = False
    End With
    
    saveLocation = "d:\sample.pdf"
    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation
    wb.Close SaveChanges:=False
    
    Set wb = Nothing
    Set ws = Nothing
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏