Excel VBA Merge PDF Files that contain strings then next string

BalloutMoe

Board Regular
Joined
Jun 4, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Hello, first time posting here. I have found plenty of helpful code and would like your help. Every month I get a list of statements and invoices that I would like to make a code that combines these pdfs and save them under the statement file. For example:
Teststatement.pdf
test1
testpart2
testpart3
testpart4
Anotherstatement.pdf
another1
anotherpart2
anotherpart3
and so on.
So I would like to merge all the test pdf files and save them under the teststatement.pdf file with the statement pages being the first. Anyway can this be done. Any help would be appreciated. Thank you
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
and second file combining all "another" invoices and saving them under anotherstatement.pdf file.
 
Upvote 0
VBA Code:
Sub mergepdf()
 Dim objCAcroPDDocDestination As Acrobat.CAcroPDDoc
    Dim objCAcroPDDocSource As Acrobat.CAcroPDDoc, LastRow As Range
    Dim PDFfiles As Range, PDFfile As Range
    Dim n As Long
    
   With ActiveWorkbook.ActiveSheet
        Set PDFfiles = .Range("A1", .Cells(.Rows.Count, "A").End(xlUp)) 'I need to make this look for a string and combine then go to next string in range
    End With
    'Create Acrobat API objects
    
    Set objCAcroPDDocDestination = CreateObject("AcroExch.PDDoc")
    Set objCAcroPDDocSource = CreateObject("AcroExch.PDDoc")
    
    'Open first PDF file and merge other PDF files into it
    
    n = 0
    For Each PDFfile In PDFfiles
        n = n + 1
        If n = 1 Then
            objCAcroPDDocDestination.Open PDFfile.Value
        Else
            objCAcroPDDocSource.Open PDFfile.Value
            If Not objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
                MsgBox "Error merging " & PDFfile.Value
            End If
            objCAcroPDDocSource.Close
        End If
    Next
    Debug.Print FileName
    'Save merged PDF files as a new file
    
    objCAcroPDDocDestination.Save 1, ActiveWorkbook.ActiveSheet.Range("M1") 'save this file under the statement file
    objCAcroPDDocDestination.Close
    
    Set objCAcroPDDocSource = Nothing
    Set objCAcroPDDocDestination = Nothing

    MsgBox "Created " & ActiveWorkbook.ActiveSheet.Range("M1")
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,451
Members
452,514
Latest member
cjkelly15

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