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

时间:2018-08-28 13:26:03

标签: excel vba excel-vba

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

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

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

1 个答案:

答案 0 :(得分:1)

几乎可以肯定,如果没有安装Adobe Acrobat,VBA将无法做到这一点。如果您有Acrobat,可以尝试一下。

import React, { Component } from 'react'
import * as BooksAPI from './BooksAPI'
import PropTypes from 'prop-types'
import Books from './Books.js'

export default class ChangeShelf extends Component {

  static propTypes = {
    book: PropTypes.object.isRequired,
    books: PropTypes.array.isRequired,
    onChangeShelf: PropTypes.func.isRequired
}

updateShelf = (book, newShelf) => {
  this.setState(previousState => {
    const newBooks = previousState.books.filter((b) => b.id !== 
book.id)
    newBooks.push({ book, newShelf })
    return { books: newBooks }
  })
  BooksAPI.update(book, newShelf)
}
moveBook = (shelf) => {
  const { books } = this.props
  return books.filter(book => book.shelf === shelf)
}

render() {
  const { book, books, updateShelf } = this.props

    return (
      <div className="book-shelf-changer">
        <select onChange={(event) => updateShelf(book, 
event.target.value)}>
          <option value="move" disabled>Move to...</option>
          <option value="Currently Reading">Currently Reading</option>
          <option value="Want To Read">Want to Read</option>
          <option value="Read">Read</option>
          <option value="none">None</option>
        </select>
      </div>
    )
  }
}

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

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

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

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