Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用VBA将工作表中的每个表导出到单个PDF中的单独页面上_Excel_Vba - Fatal编程技术网

Excel 使用VBA将工作表中的每个表导出到单个PDF中的单独页面上

Excel 使用VBA将工作表中的每个表导出到单个PDF中的单独页面上,excel,vba,Excel,Vba,我可以创建一个宏来将每个表导出到它们自己的PDF中,但是有没有办法将每个表放在它自己的页面上的单个PDF中 Range("B2:C5").Select ActiveSheet.PageSetup.PrintArea = "$B$2:$B$5" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ "C:\Users\Lenovo\Documents\Book1.pdf", Quality:=xlQualityStand

我可以创建一个宏来将每个表导出到它们自己的PDF中,但是有没有办法将每个表放在它自己的页面上的单个PDF中

Range("B2:C5").Select
ActiveSheet.PageSetup.PrintArea = "$B$2:$B$5"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\Lenovo\Documents\Book1.pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
    True
Range("B2:C6").Select

感谢所有建议。

几年前,我一直在尝试合并PDF,并将我选择的任何PDF合并为1

这是我的代码的合并部分,您需要根据自己的需要进行调整:

Sub MergePDFs()
Dim a As Variant, i As Long, n As Long, ni As Long, p As String
Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc

MyPath = Range("G1").Text
DestFile = Range("G2").Text
MyFiles = Join(WorksheetFunction.Transpose(Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)), ",")

  If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\"
  a = Split(MyFiles, ",")
  ReDim PartDocs(0 To UBound(a))

  On Error GoTo exit_
  If Len(Dir(p & DestFile)) Then Kill p & DestFile
  For i = 0 To UBound(a)
    ' Check PDF file presence
    If Dir(p & Trim(a(i))) = "" Then
      MsgBox "File not found" & vbLf & p & a(i), vbExclamation, "Canceled"
      Exit For
    End If
    ' Open PDF document
    Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
    PartDocs(i).Open p & Trim(a(i))
    If i Then
      ' Merge PDF to PartDocs(0) document
      ni = PartDocs(i).GetNumPages()
      If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then
        MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
      End If
      ' Calc the number of pages in the merged document
      n = n + ni
      ' Release the memory
      PartDocs(i).Close
      Set PartDocs(i) = Nothing
    Else
      ' Calc the number of pages in PartDocs(0) document
      n = PartDocs(0).GetNumPages()
    End If
  Next

  If i > UBound(a) Then
    ' Save the merged document to DestFile
    If Not PartDocs(0).Save(PDSaveFull, p & DestFile) Then
      MsgBox "Cannot save the resulting document" & vbLf & p & DestFile, vbExclamation, "Canceled"
    End If
  End If

exit_:

  ' Inform about error/success
  If Err Then
    MsgBox Err.Description, vbCritical, "Error #" & Err.Number
  ElseIf i > UBound(a) Then
    MsgBox "The resulting file is created:" & vbLf & p & DestFile, vbInformation, "Done"
  End If

  ' Release the memory
  If Not PartDocs(0) Is Nothing Then PartDocs(0).Close
  Set PartDocs(0) = Nothing

  ' Quit Acrobat application
  AcroApp.Exit
  Set AcroApp = Nothing

End Sub

根据记忆,我没有在我的合并工具中编写这个例程,但我记不起是从哪里得到的,所以我不能相信原来的编码器。

几年前,我一直在玩合并PDF的游戏,并将我选择的任何PDF合并为1

这是我的代码的合并部分,您需要根据自己的需要进行调整:

Sub MergePDFs()
Dim a As Variant, i As Long, n As Long, ni As Long, p As String
Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc

MyPath = Range("G1").Text
DestFile = Range("G2").Text
MyFiles = Join(WorksheetFunction.Transpose(Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)), ",")

  If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\"
  a = Split(MyFiles, ",")
  ReDim PartDocs(0 To UBound(a))

  On Error GoTo exit_
  If Len(Dir(p & DestFile)) Then Kill p & DestFile
  For i = 0 To UBound(a)
    ' Check PDF file presence
    If Dir(p & Trim(a(i))) = "" Then
      MsgBox "File not found" & vbLf & p & a(i), vbExclamation, "Canceled"
      Exit For
    End If
    ' Open PDF document
    Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
    PartDocs(i).Open p & Trim(a(i))
    If i Then
      ' Merge PDF to PartDocs(0) document
      ni = PartDocs(i).GetNumPages()
      If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then
        MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
      End If
      ' Calc the number of pages in the merged document
      n = n + ni
      ' Release the memory
      PartDocs(i).Close
      Set PartDocs(i) = Nothing
    Else
      ' Calc the number of pages in PartDocs(0) document
      n = PartDocs(0).GetNumPages()
    End If
  Next

  If i > UBound(a) Then
    ' Save the merged document to DestFile
    If Not PartDocs(0).Save(PDSaveFull, p & DestFile) Then
      MsgBox "Cannot save the resulting document" & vbLf & p & DestFile, vbExclamation, "Canceled"
    End If
  End If

exit_:

  ' Inform about error/success
  If Err Then
    MsgBox Err.Description, vbCritical, "Error #" & Err.Number
  ElseIf i > UBound(a) Then
    MsgBox "The resulting file is created:" & vbLf & p & DestFile, vbInformation, "Done"
  End If

  ' Release the memory
  If Not PartDocs(0) Is Nothing Then PartDocs(0).Close
  Set PartDocs(0) = Nothing

  ' Quit Acrobat application
  AcroApp.Exit
  Set AcroApp = Nothing

End Sub

根据记忆,我没有在我的合并工具中编写此例程,但我不记得从何处获得它,因此我无法信任原始编码器。

在设置每个工作表页面设置后,然后导出工作簿

Sub test()

    Dim WB As Workbook, Ws As Worksheet
    Dim myFn As String
    Set WB = ThisWorkbook
    For Each Ws In Worksheets
        Ws.PageSetup.PrintArea = "$B$2:$B$5"
    Next Ws
    myFn = WB.Path & "\" & "test.pdf"

    WB.ExportAsFixedFormat xlTypePDF, myFn
End Sub

设置每个工作表页面设置后,导出工作簿

Sub test()

    Dim WB As Workbook, Ws As Worksheet
    Dim myFn As String
    Set WB = ThisWorkbook
    For Each Ws In Worksheets
        Ws.PageSetup.PrintArea = "$B$2:$B$5"
    Next Ws
    myFn = WB.Path & "\" & "test.pdf"

    WB.ExportAsFixedFormat xlTypePDF, myFn
End Sub

谢谢,玩了几分钟后,我重新构建了电子表格,将事件表放在单独的工作表上,并使用了您的代码。。。可以说,它现在是一个更强大的解决方案,将来更容易升级。为此,在玩了几分钟后,我重新构建了电子表格,将事件表放在单独的工作表上,并使用了您的代码。。。它现在可以说是一个更强大的解决方案,将来更容易升级。我现在已经玩过了,我正在将它添加到我自己的库中,谢谢!我现在已经玩过了,我正在把它添加到我自己的库中,谢谢!