【问题标题】:Saving more than 1 excel sheet as a single PDF将超过 1 个 Excel 工作表保存为单个 PDF
【发布时间】:2017-11-11 03:34:47
【问题描述】:

我希望将 2 个以上的 Excel 工作表保存为一个 PDF 文件。我有这个代码,但它只能保存单个文件,如何使它工作,以便它可以选择 2 个文件并将其保存为单个 PDF。

Sub CMSaveAsPDF()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant

On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = Worksheets("Design")
strPath = wbA.path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

strFile = "Design" 
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
    FileFilter:="PDF Files (*.pdf), *.pdf", _
    Title:="Select Folder and FileName to save")

      If myFile <> "False" Then
      wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If
End sub

【问题讨论】:

  • 如果手动,我们选择两张表,然后只保存为pdf,您的代码也可以这样做
  • 感谢您的建议,我使用宏捕获代码并在数组中添加了工作表名称,它工作正常。Sheets(Array("Design", "Data")).Select

标签: vba excel pdf


【解决方案1】:

选择多张表后,导出Activesheet。

Sub CMSaveAsPDF()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant

On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = Worksheets("Design")
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

strFile = "Design"
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
    FileFilter:="PDF Files (*.pdf), *.pdf", _
    Title:="Select Folder and FileName to save")

      If myFile <> "False" Then
      Sheets(Array("Design", "Data")).Select 'first multi sheets select
      'change to Activesheet
      ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If
End Sub

【讨论】:

    【解决方案2】:
    Sub CMSaveAsPDF()
    Dim wsA As Worksheet
    Dim wbA As Workbook
    Dim strPath As String
    Dim strFile As String
    Dim strPathFile As String
    Dim myFile As Variant
    
    On Error GoTo errHandler
    Set wbA = ActiveWorkbook
    Set wsA = Worksheets("Design")
    strPath = wbA.path
    If strPath = "" Then
    strPath = Application.DefaultFilePath
    End If
    strPath = strPath & "\"
    
    strFile = "Design" 
    myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")
    
         If myFile <> "False" Then
         Sheets(Array("Design", "Data")).Select  ' Selected sheet names in array
          wsA.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=myFile, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
        'confirmation message with file info
        MsgBox "PDF file has been created: " _
          & vbCrLf _
          & myFile
    End If
    End sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 2020-12-13
      相关资源
      最近更新 更多