Merge only specific page from multiple pdf files in folder and save in the same folder with file name "Merged"

nbuddhi

New Member
Joined
Jun 23, 2020
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Dear Team,

I have around 700 pdf files in a folder and each file having 10-20 pages, so that I want to merge only the page number 2 from all the files and merge them to a pdf file called "Merged" or merged data save as a excel file in same source folder. Kindly help me to find a macro to do this job easily. I'm using Acrobar Pro (32bit).
Thank you.

Thanks in advaance,
nbuddhi
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
See if you can adapt this code:

Change these lines to merge only page 2 (instead of all pages) from each source PDF to the destination PDF:
VBA Code:
If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then

':
':

n = n + ni

To:
VBA Code:
If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 1, 1, True) Then

':
':

n = n + 1
(InsertPages 3rd argument is the starting page number (0 based) in the source PDF and the 4th argument is the number of pages to insert).
 
Upvote 0
Dear John,

Many thanks for your support. I tried the code, but there is a compile error popup as "user-defined type not defined" highlighting section "AcroApp As New Acrobat.AcroApp". Greatly appreciate if you can help to overcome this error.

Thanks,
nbuddhi
 
Upvote 0
You must set a reference to Adobe Acrobat nn.0 Type Library, via Tools -> References in the VBA editor.
 
Upvote 0
Dear John,

Many Thanks. It worked. I tried revising the code as you suggested, but it merged all the pages, then I removed ni & n arguments and the same area TRUE argument changed as FALSE. Then result was ok as second page of all the files inserted after second page of first pdf and I can manually delete those additional few pages of first pdf. Many thanks for your support and really appreciated.

Thanks,
nbuddhi

VBA Code:
            If Not PartDocs(0).InsertPages(-1, PartDocs(i), 1, 1, False) Then
            MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
 
Upvote 0
Then result was ok as second page of all the files inserted after second page of first pdf and I can manually delete those additional few pages of first pdf.

I haven't looked closely at the code, but I think that happens because it takes the first PDF as the initial merged document and merges the pages from all the other PDFs to it. You need to create a new empty PDF first and then merge page 2 from all the other PDFs to it, as in the following macro. Change the 2 strings near the top of the code for the PDFs you want to merge and the full name of the merged PDF output.

VBA Code:
Public Sub Merge_Page_2_From_All_PDFs()

    Dim PDDocDestination As Acrobat.AcroPDDoc
    Dim PDDocSource As Acrobat.AcroPDDoc
    Dim matchPDFs As String, mergedPDF As String
    Dim folder As String, PDFfile As String
    
    matchPDFs = "C:\path\to\*.pdf"       'CHANGE THIS
   
    mergedPDF = "C:\another\path\Merged.pdf"     'CHANGE THIS
    
    'Delete destination (merged) PDF if it already exists - a new PDF will be created
    
    If Dir(mergedPDF) <> vbNullString Then Kill mergedPDF
    
    'Create Acrobat API objects
    
    Set PDDocDestination = New Acrobat.AcroPDDoc 'CreateObject("AcroExch.PDDoc")
    Set PDDocSource = New Acrobat.AcroPDDoc 'CreateObject("AcroExch.PDDoc")
    
    'Create new destination (merged) PDF
    
    PDDocDestination.Create
    
    'Loop through matching PDFs and insert page 2 from each PDF to the merged PDF
    
    folder = Left(matchPDFs, InStrRev(matchPDFs, "\"))
    PDFfile = Dir(matchPDFs)
    While PDFfile <> vbNullString
        PDDocSource.Open folder & PDFfile
        If Not PDDocDestination.InsertPages(PDDocDestination.GetNumPages - 1, PDDocSource, 1, 1, 0) Then
            MsgBox "Error merging" & vbCrLf & folder & PDFfile & vbCrLf & "to" & vbCrLf & mergedPDF, vbExclamation
        End If
        PDDocSource.Close
        PDFfile = Dir
    Wend
    
    'Save destination (merged) PDF
    
    PDDocDestination.Save 1, mergedPDF
    PDDocDestination.Close
       
    Set PDDocSource = Nothing
    Set PDDocDestination = Nothing

    MsgBox "Created " & mergedPDF
    
End Sub
the same area TRUE argument changed as FALSE

The 5th argument to InsertPages determines whether bookmarks are copied from the source PDF. A positive number means they are. Zero means they are not. (Therefore True in the linked code is invalid because it evalulates to -1 in VBA; False is valid because it evaluates to 0.)
 
Upvote 0
Solution
I haven't looked closely at the code, but I think that happens because it takes the first PDF as the initial merged document and merges the pages from all the other PDFs to it. You need to create a new empty PDF first and then merge page 2 from all the other PDFs to it, as in the following macro. Change the 2 strings near the top of the code for the PDFs you want to merge and the full name of the merged PDF output.

VBA Code:
Public Sub Merge_Page_2_From_All_PDFs()

    Dim PDDocDestination As Acrobat.AcroPDDoc
    Dim PDDocSource As Acrobat.AcroPDDoc
    Dim matchPDFs As String, mergedPDF As String
    Dim folder As String, PDFfile As String
   
    matchPDFs = "C:\path\to\*.pdf"       'CHANGE THIS
  
    mergedPDF = "C:\another\path\Merged.pdf"     'CHANGE THIS
   
    'Delete destination (merged) PDF if it already exists - a new PDF will be created
   
    If Dir(mergedPDF) <> vbNullString Then Kill mergedPDF
   
    'Create Acrobat API objects
   
    Set PDDocDestination = New Acrobat.AcroPDDoc 'CreateObject("AcroExch.PDDoc")
    Set PDDocSource = New Acrobat.AcroPDDoc 'CreateObject("AcroExch.PDDoc")
   
    'Create new destination (merged) PDF
   
    PDDocDestination.Create
   
    'Loop through matching PDFs and insert page 2 from each PDF to the merged PDF
   
    folder = Left(matchPDFs, InStrRev(matchPDFs, "\"))
    PDFfile = Dir(matchPDFs)
    While PDFfile <> vbNullString
        PDDocSource.Open folder & PDFfile
        If Not PDDocDestination.InsertPages(PDDocDestination.GetNumPages - 1, PDDocSource, 1, 1, 0) Then
            MsgBox "Error merging" & vbCrLf & folder & PDFfile & vbCrLf & "to" & vbCrLf & mergedPDF, vbExclamation
        End If
        PDDocSource.Close
        PDFfile = Dir
    Wend
   
    'Save destination (merged) PDF
   
    PDDocDestination.Save 1, mergedPDF
    PDDocDestination.Close
      
    Set PDDocSource = Nothing
    Set PDDocDestination = Nothing

    MsgBox "Created " & mergedPDF
   
End Sub


The 5th argument to InsertPages determines whether bookmarks are copied from the source PDF. A positive number means they are. Zero means they are not. (Therefore True in the linked code is invalid because it evalulates to -1 in VBA; False is valid because it evaluates to 0.)
Many Thanks John. This code is perfect.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top