使用excel vba合并文件夹中的所有pdf文件

诺贝尔奖

在一个文件夹中,我只有最初通过导出 excel 工作表创建的 pdf 文件。现在我想将这些文件合并为一个并通过电子邮件发送。

我知道如何在 excel vba 中通过电子邮件发送单个文档。但我完全不知道如何进行合并部分。有人可以帮助我吗?

在互联网上搜索时,我遇到了不同的网站,例如https://wellsr.com/vba/2017/word/combine-pdfs-with-vba-and-adobe-acrobat/或多或少地处理这个问题. 但我真的不明白这些代码......

我几乎可以肯定,如果没有安装 Adob​​e Acrobat,VBA 将无法做到这一点。如果你有 Acrobat,你可以试试这个。

Sub MergePDFs()
' ZVI:2013-08-27 http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X
' Reference required: "VBE - Tools - References - Acrobat"

  ' --> Settings, change to suit
  Const MyPath = "C:\Temp"            ' Path where PDF files are stored
   Const MyFiles = "1.pdf,2.pdf,3.pdf"  ' List of PDFs to ne merged
  Const DestFile = "MergedFile.pdf"   ' The name of the merged file
  ' <-- End of settings

  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

  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

http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X

此外,您可以将 Python 用于此类任务,而无需任何特殊或昂贵的附加应用程序。

import glob
from PyPDF2 import PdfFileWriter, PdfFileReader

def merger(output_path, input_paths):
    pdf_writer = PdfFileWriter()

    for path in input_paths:
        pdf_reader = PdfFileReader(path)
        for page in range(pdf_reader.getNumPages()):
            pdf_writer.addPage(pdf_reader.getPage(page))

    with open(output_path, 'wb') as fh:
        pdf_writer.write(fh)


if __name__ == '__main__':
    paths = glob.glob('C:/your_path_here/*.pdf')
    paths.sort()
    merger('pdf_merger.pdf', paths)

https://www.blog.pythonlibrary.org/2018/04/11/splitting-and-merging-pdfs-with-python/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用PHP从文件夹中删除所有文件?

使用VBA获取文件夹中的Excel文件列表

如何使用.gitignore忽略文件夹中的所有子文件夹

使用GraphQL查询文件夹中的所有图像

如何使用python删除文件夹中的所有文件夹?

spark无法使用合并模式从不同文件夹中读取所有orc文件

VBA代码使用WQL查找目录中的所有隐藏文件夹并将其存储在xml文件中

使用文件夹名称重命名文件夹中的所有图像

使用pypdf2将文件夹中的所有pdf文件合并为一个pdf

如何使用python和os合并特定文件夹中的所有csv文件

循环浏览文件夹中的所有Excel文件,并使用SAS提取每个文件的特定单元格

使用R将文件夹中的所有PDF和所有子文件夹复制到新文件夹

使用VBA在文件夹中的所有文档中查找和替换MS Word文本

遍历文件夹并使用文件夹名称顺序重命名每个文件夹中的所有文件

使用批处理删除文件夹中除列表中的文件以外的所有文件

如何在文件夹中的所有(关闭)Excel文件上运行一个VBA宏?

如何使用PowerShell从文件夹和所有子文件夹中删除所有访问规则?

如何在文件夹中合并所有JS文件并在GruntJS中使用SASS?

如何使用Groovy读取文件夹中的所有文件并替换文件中的模式

Excel VBA代码将文件夹中的rtf文件批量转换为pdf文件

使用 PowerShell 打印文件夹中的所有 Excel 文件

使用文件名为文件夹中的所有pdf添加水印

在 Python 中浏览和合并文件夹及其子文件夹中的 Excel 文件

Excel VBA将特定文件夹中的所有Word文件转换为PDF

如何使用bash将子文件夹中的所有文件移动到新文件夹中?

Excel VBA代码将父文件夹中的所有文件名捕获到excel表中

Applescript 使用预览将文件夹中的所有文件导出为 pdf

打开文件夹中的所有 Excel 文件并使用 PowerShell 保存

如何使用命令 COPY 将文件夹中的所有 MP3 文件合并为一个 MP3 文件?