Loop through files in folder that match part filename

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I'm tinkering with this in an attempt to merge all pdf files in a folder into 1;

Code:
Sub MergePDF()

'Relies on the Adobe Acrobat 6.0 Type Library
Dim objCAcroPDDocDestination As Acrobat.CAcroPDDoc
Dim objCAcroPDDocSource As Acrobat.CAcroPDDoc

'Initialize the objects
Set objCAcroPDDocDestination = CreateObject("AcroExch.PDDoc")
Set objCAcroPDDocSource = CreateObject("AcroExch.PDDoc")

'Open Destination, all other documents will be added to this and saved with
'a new filename
objCAcroPDDocDestination.Open (ThisWorkbook.Path & "\Contact Logs\Record of Contact - Log Number 1.pdf")

'Do your loop here to open subsequent documents that you want to add
'Do
  'Open the source document that will be added to the destination
  objCAcroPDDocSource.Open (ThisWorkbook.Path & "\Contact Logs\Record of Contact - Log Number 2.pdf")
  If objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
    MsgBox "Documents Merged!"
  Else
    '0 problem
  End If
  objCAcroPDDocSource.Close
'loop

objCAcroPDDocDestination.Save 1, ThisWorkbook.Path & "\Contact Logs\Record of Contact (Complete).pdf"
objCAcroPDDocDestination.Close
Set objCAcroPDDocSource = Nothing
Set objCAcroPDDocDestination = Nothing

End Sub

It works fine, and in my testing I've specified Log numbers 1 and 2, but I want to be able to merge all files in the folder that have 'Log Number' in the filename and merge them in order, (i.e 1, 2, 3 onwards etc). I guess I would need to add something after this line;

Code:
'Do your loop here to open subsequent documents that you want to add
'Do

but I don't know how to if anyone can point me in the right direction please?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this loop:
Code:
    Dim n As Long, PDFfileName As String
    
    n = 1
    Do
        n = n + 1
        PDFfileName = Dir(ThisWorkbook.Path & "\Contact Logs\Record of Contact - Log Number " & n & ".pdf")
        If PDFfileName <> "" Then
            'Open the source document that will be added to the destination
            objCAcroPDDocSource.Open ThisWorkbook.Path & "\Contact Logs\" & PDFfileName
            If objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
                MsgBox "Merged " & PDFfileName
            Else
                MsgBox "Error merging " & PDFfileName
            End If
            objCAcroPDDocSource.Close
        End If
    Loop While PDFfileName <> ""
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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