使用Access VBA问题将PDF合并为一个PDF文件

问题描述

我目前正在使用此代码合并我的Access报告,这些报告打印为.pdf。直到最近它一直运行良好,我不确定为什么不合并我的.pdf文件。有人可以看一下,然后告诉我哪里出了问题。我的参考设置为Adobe Acrobat 10.0类型库。任何帮助将不胜感激。

Dim Encl2,Encl2FN,Encl2Path,Encl3,Encl3FN,Encl3Path,Encl4,Encl4FN,Encl4Path As String
Dim RPath,TempPath,TempPathEncl2,TempPathEncl3,TempPathEncl4 As String
Dim UserPth,strfile,strFilePath As String
Dim arrayFilePaths() As Variant
Dim app As Acrobat.CAcroApp
Dim primaryDoc As Acrobat.CAcroPDDoc
Dim SourceDoc As Acrobat.CAcroPDDoc
Dim OK
Dim arrayIndex
Dim numberOfPagesToInsert As String
Dim numPages As Integer

Encl2 = "rpt_Delegation_Enclosure2"
Encl3 = "rpt_Delegation_Enclosure3"
Encl4 = "rpt_Delegation_Enclosure4"

Encl2FN = "Enclosure2" & ".pdf"
Encl3FN = "Enclosure3" & ".pdf"
Encl4FN = "Enclosure4" & ".pdf"

UserPth = Environ("USERPROFILE")
TempPathEncl2 = UserPth & "\Desktop\" & Encl2FN
TempPathEncl3 = UserPth & "\Desktop\" & Encl3FN
TempPathEncl4 = UserPth & "\Desktop\" & Encl4FN
strfile = "SourceRpt" & ".pdf"
strFilePath = UserPth & "\Desktop\" & strFile

RPath = strFilePath

    Set app = CreateObject("AcroExch.App")

    arrayFilePaths = Array(RPath,TempPathEncl4)

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(arrayFilePaths(0))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK
            
    For arrayIndex = 1 To UBound(arrayFilePaths)
        numPages = primaryDoc.GetNumPages() - 1

        Set SourceDoc = CreateObject("AcroExch.PDDoc")
        OK = SourceDoc.Open(arrayFilePaths(arrayIndex))
        Debug.Print "SOURCE DOC OPENED & PDDOC SET: " & OK

        numberOfPagesToInsert = SourceDoc.GetNumPages

        OK = primaryDoc.InsertPages(numPages,SourceDoc,numberOfPagesToInsert,False)
        Debug.Print "PAGES INSERTED SUCCESSFULLY: " & OK

        OK = primaryDoc.Save(PDSaveFull,arrayFilePaths(0))
        Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK
        
        Set SourceDoc = nothing
    Next arrayIndex

    Set primaryDoc = nothing
    app.Exit
    Set app = nothing
End Sub

解决方法

这对我来说很好

改编自:VBA,Combine PDFs into one PDF file

Sub main()

    Const PDSaveFull = 1
    Dim arrayFilePaths() As Variant,OK,arrayIndex As Long
    Dim primaryDoc,app,numPages,numberOfPagesToInsert,sourceDoc
    
    Set app = CreateObject("Acroexch.app")

    arrayFilePaths = Array("C:\Tester\PDF_Sheet1.pdf",_
                            "C:\Tester\PDF_Sheet2.pdf",_
                            "C:\Tester\PDF_Sheet3.pdf")

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(arrayFilePaths(0))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For arrayIndex = 1 To UBound(arrayFilePaths)
        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(arrayFilePaths(arrayIndex))
        Debug.Print "SOURCE DOC OPENED & PDDOC SET: " & OK

        numberOfPagesToInsert = sourceDoc.GetNumPages

        OK = primaryDoc.InsertPages(numPages,sourceDoc,False)
        Debug.Print "PAGES INSERTED SUCCESSFULLY: " & OK

        OK = primaryDoc.Save(PDSaveFull,arrayFilePaths(0))
        Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

        Set sourceDoc = Nothing
    Next arrayIndex

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing
    MsgBox "DONE"
End Sub