Combine two PDF into one Separate PDF and save in particular path Using VBA
Results 1 to 6 of 6

Thread: Combine two PDF into one Separate PDF and save in particular path Using VBA

  1. #1
    Board Regular
    Join Date
    Mar 2019
    Location
    India
    Posts
    170
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Combine two PDF into one Separate PDF and save in particular path Using VBA

    Hi Everyone,
    Here i am trying to combine two PDF sheets using Adobe, where the below code runs perfectly but
    Code:
    Sub Combine_PDF()
        Dim x, lastrow As Long
        Dim arrayFilePaths() As Variant
        Set app = CreateObject("Acroexch.app")
        
        lastrow = Sheets("Combine_PDF").Cells(Rows.Count, 1).End(xlUp).Row
        For x = 2 To lastrow
            path1 = Cells(x, "A").Value
            path2 = Cells(x, "B").Value
            arrayFilePaths = Array(path1, path2)
            
            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, 0, 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
        Next x
        app.Exit
        Set app = Nothing
        MsgBox "DONE"
    End Sub
    but can anyone suggest me how i can combine two PDF sheets and save that sheet in separate PDF file and in separate path
    Is their any possible ways to do that?????
    Please do suggest me with this

    Regards
    Dhruv
    Last edited by GirishDhruva; Jun 7th, 2019 at 03:39 AM.

  2. #2
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,807
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: How can we combine two PDF into one Separate PDF and save in particular path Using VBA

    Try this macro, which expects the full path and file name of the 2 input PDFs to be in column A and B and the output PDF to be in column C. Or change every PDFs(i, 3) to PDFs(i, 1) if you want the output PDF to be the same as column A (overwriting the original file).

    Code:
    Public Sub Combine_PDFs()
        
        Dim PDFs As Variant, i As Long
        Dim primaryDoc As Object, sourceDoc As Object
        Dim numPages As Long, numberOfPagesToInsert As Long
        
        With Sheets("Combine_PDF")
            PDFs = .Range("A2:C" & .Cells(.Rows.Count, 1).End(xlUp).Row)
        End With
        
        Set primaryDoc = CreateObject("AcroExch.PDDoc")
        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        
        For i = 1 To UBound(PDFs)
            If primaryDoc.Open(PDFs(i, 1)) Then
                numPages = primaryDoc.GetNumPages() - 1
                If sourceDoc.Open(PDFs(i, 2)) Then
                    numberOfPagesToInsert = sourceDoc.GetNumPages
                    If primaryDoc.InsertPages(numPages, sourceDoc, 0, numberOfPagesToInsert, False) Then
                        If primaryDoc.Save(PDSaveFlags.PDSaveFull, PDFs(i, 3)) Then
                            MsgBox "Created " & PDFs(i, 3)
                        Else
                            MsgBox "Error saving " & PDFs(i, 3)
                        End If
                    Else
                        MsgBox "Error inserting pages from " & PDFs(i, 2) & " into " & PDFs(i, 1)
                    End If
                    sourceDoc.Close
                Else
                    MsgBox "Error opening Source PDF " & PDFs(i, 2)
                End If
                primaryDoc.Close
            Else
                MsgBox "Error opening Primary PDF " & PDFs(i, 1)
            End If
        Next
        
        Set sourceDoc = Nothing
        Set primaryDoc = Nothing
        
        MsgBox "DONE"
        
    End Sub

  3. #3
    Board Regular
    Join Date
    Mar 2019
    Location
    India
    Posts
    170
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How can we combine two PDF into one Separate PDF and save in particular path Using VBA

    Hi John_w thanks for your reply, I am out for next two days so once I reach back I will check and confirm you

    Regards
    Dhruv

  4. #4
    Board Regular
    Join Date
    Mar 2019
    Location
    India
    Posts
    170
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How can we combine two PDF into one Separate PDF and save in particular path Using VBA

    Hi John_w it's throwing me an run time error 424 as object required in below line
    Code:
    If primaryDoc.Save(PDSaveFlags.PDSaveFull, PDFs(i, 3)) Then
    even i tried with
    Code:
    If primaryDoc.Save(PDSaveFlags.PDSaveFull, PDFs(i, 1)) Then
    then also same error

    Regards
    Dhruv

  5. #5
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,807
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: How can we combine two PDF into one Separate PDF and save in particular path Using VBA

    I guess you're not using Option Explicit and haven't set a reference to the Adobe Acrobat Type Library. In that case, change PDSaveFlags.PDSaveFull to 1 and it should work.

    You should always use Option Explicit at the top of every module because it forces you to declare (Dim) variables and makes investigating compilation errors and run-time errors such as this one far easier. To have Option Explicit included automatically at the top of every module, in the VBA editor click Tools - Options -> Editor tab - tick Require Variable Declaration.

  6. #6
    Board Regular
    Join Date
    Mar 2019
    Location
    India
    Posts
    170
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How can we combine two PDF into one Separate PDF and save in particular path Using VBA

    Quote Originally Posted by John_w View Post
    I guess you're not using Option Explicit and haven't set a reference to the Adobe Acrobat Type Library. In that case, change PDSaveFlags.PDSaveFull to 1 and it should work.

    You should always use Option Explicit at the top of every module because it forces you to declare (Dim) variables and makes investigating compilation errors and run-time errors such as this one far easier. To have Option Explicit included automatically at the top of every module, in the VBA editor click Tools - Options -> Editor tab - tick Require Variable Declaration.
    Hi @John_w ,It worked and thanks for you valuable information and suggestions and sure from now i will always use Option Explicit.

    Regards
    Dhruv

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •