Replace pages of one PDF with pages of another PDF using VBA and Adobe 10

elbrodero

New Member
Joined
Jun 8, 2020
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
I am trying to use VBA to replace pages of part1.pdf with pages of part2.pdf and create mergedpdf.pdf.

I tried the following code:


VBA Code:
Sub Button1_Click()

    Dim AcroApp As Acrobat.CAcroApp

    Dim Part1Document As Acrobat.CAcroPDDoc
    Dim Part2Document As Acrobat.CAcroPDDoc

    Dim numPages As Integer

    Set AcroApp = CreateObject("AcroExch.App")

    Set Part1Document = CreateObject("AcroExch.PDDoc")
    Set Part2Document = CreateObject("AcroExch.PDDoc")

    Doc1.Open ("C:\temp\Part1.pdf")
    Doc2.Open ("C:\temp\Part2.pdf")

    ' Insert the pages of Part2 after the end of Part1
    numPages = Doc1.GetNumPages()

    If Doc1.InsertPages(numPages 3, Doc2,
        0, Doc2.GetNumPages(), True) = False Then
        MsgBox "Cannot insert pages"
    End If

    If Doc1.Save(PDSaveFull, "C:\temp\MergedFile.pdf") = False Then
        MsgBox "Cannot save the modified document"
    End If

    Doc1.Close
    Doc2.Close

    AcroApp.Exit
    Set AcroApp = Nothing
    Set Part1Document = Nothing
    Set Part2Document = Nothing

    MsgBox "Done"

End Sub

What the code does right now is open doc1.pdf and save it as MergedFile.pdf. But it does not replace pages. Can anyone help me out?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Welcome to MrExcel forums.

See if the following code helps, noting that it merges 2 or more PDF files specified in Excel cells, rather than files specified in the code:
 
Upvote 0
Try this bare code, no error handling.

VBA Code:
Sub delete_insert()

 Dim AcroApp As Acrobat.CAcroApp

    Dim Part1Document As Acrobat.CAcroPDDoc
    Dim Part2Document As Acrobat.CAcroPDDoc
    Dim i As Integer
    Dim j As Integer
    Dim numPages As Integer

    Set AcroApp = CreateObject("AcroExch.App")

    Set Part1Document = CreateObject("AcroExch.PDDoc")
    Set Part2Document = CreateObject("AcroExch.PDDoc")

    Part1Document.Open ("C:\temp\test1.pdf")
    Part2Document.Open ("C:\temp\test2.pdf")
    
    i = Part1Document.DeletePages(0, 4)  'delete page 1 to 5. note: the first page is 0
    
    j = Part1Document.InsertPages(1, Part2Document, 0, 2, 0) ' insert page 1 and 2 in Part2Document into Part1Document starting from after the 2nd page

    If Part1Document.Save(PDSaveFull, "C:\temp\MergedFile.pdf") = False Then
        MsgBox "Cannot save the modified document"
    End If

    Part1Document.Close
    Part2Document.Close

    AcroApp.Exit
    Set AcroApp = Nothing
    Set Part1Document = Nothing
    Set Part2Document = Nothing

    MsgBox "Done"

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,639
Messages
6,173,498
Members
452,516
Latest member
druck21

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