【问题标题】:VBA Excel - Export PDF file from Excel with second pageVBA Excel - 从带有第二页的 Excel 导出 PDF 文件
【发布时间】:2023-03-28 18:45:01
【问题描述】:

只是想问一下如何用vba导出PDF文件?问题是我确实有一张 10-F10-B 表。下面的代码在 10-F 表中工作。我的问题是如何将 10-B 表中的数据与 10-F 一起导出?第一页是10-F的数据,10-B的数据在第二页。

10-B 工作表的范围是"B10:AD92"

Sub Ver_PDF()

        'Create and assign variables
        Dim saveLocation As String
        Dim rng As Range
        lname = ThisWorkbook.Sheets("HOME").Range("K12")
        fname = ThisWorkbook.Sheets("HOME").Range("K13")
        Name = fname & " " & lname
        
        pdfile = "V-" & Name & ".pdf"
        
        saveLocation = ThisWorkbook.Path & "\V-PDF\" & pdfile
        Set rng = Sheets("10-F").Range("B9:AD89")
        
        Dim strFileExists As String

        strFileExists = Dir(saveLocation)
    
        If strFileExists <> "" Then
        
            Dim Ret
            '~~> Change this to the relevant file path and name
            Ret = IsFileOpen(saveLocation)
            
            If Ret = True Then
                MsgBox "Please close the PDF file before proceeding...", vbCritical + vbOKOnly, "Error"
                Exit Sub
            End If
    
        End If
        
        rng.ExportAsFixedFormat Type:=xlTypePDF, _
        FileName:=saveLocation, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
        
End Sub

非常感谢任何帮助!谢谢!

【问题讨论】:

  • 在打印之前,添加一个新的工作表,比如 10-print,将 10-B 和 10-F 范围复制并粘贴到 10-print,然后导出为 pdf 并删除 10-print。

标签: excel vba pdf


【解决方案1】:

此代码将执行您想要的操作。更改数组中的工作表名称以及目标路径和文件名。

Sub ExportAsPDF()

    Dim FolderPath  As String
    Dim FileName    As String
    
    FolderPath = "D:\Test PDFs\"            ' change to suit: end on back-slash
    FileName = "Test"                       ' change to suit
    On Error Resume Next
    MkDir FolderPath
    
    On Error GoTo 0
    Worksheets(Array("10-F", "10-B")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                                    FileName:=FolderPath & FileName & ".PDF", _
                                    OpenAfterPublish:=True, _
                                    IgnorePrintAreas:=False

    MsgBox "PDF was successfully created."
    Worksheets(1).Select
End Sub

如果您不想立即看到结果,请将 OpenAfterPublish 更改为 False

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多