使用Excel 2016 VBA和Adobe Acrobat DC合并PDF

时间:2018-08-09 20:28:00

标签: excel vba pdf-generation acrobat excel-2016

我有一个Excel 2016宏,可以使用Adobe Acrobat X将PDF文档合并或连接在一起。我不再拥有Acrobat X,它被Adobe Acrobat DC取代。因此,我收到一条错误消息,指出Excel正在等待另一个应用程序完成一个仅会继续提示的操作。发生的行是:

Set PartDocs(i) = CreateObject("AcroExch.PDDoc")

我假设我需要使用Acrobat DC方法打开或创建PDF文档的其他方法,但是不确定是什么方法。有人可以协助我过渡到使用DC版本吗?我试图查找信息,但是无法看到DC版本的打开/创建PDF。

谢谢您的帮助!

Sub MergePDFs(MyFiles As String, DestFile As String, DestPath As String)
     ' 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"

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

    p = Environ("temp") & "\"
    a = Split(MyFiles, ",")

    ReDim PartDocs(0 To UBound(a))

    On Error GoTo exit_

    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, DestPath & DestFile & ".PDF") 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 & DestPath & DestFile & ".PDF", vbInformation, "Done"
        strErrorMessage = strErrorMessage & "The resulting file is created:" & vbLf & DestPath & DestFile & ".PDF" & Chr(13) & Chr(13)

    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

0 个答案:

没有答案