Skip to main content
Participant
June 8, 2020
Question

Replacing pdf pages using VBA

  • June 8, 2020
  • 2 replies
  • 1613 views

Hello guys,

 

I've been tryin to use the code of Karl Heinz to replace pages between two PDFs using excel. Unfortunately, it doesn't work. All it does on my system is save the first pdf under a different name without having exchanges any pages.

Here is the 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

 

This topic has been closed for replies.

2 replies

Bernd Alheit
Community Expert
Community Expert
June 8, 2020

Why does you use numPages 3 at InsertPages?

 

 

Thom Parker
Community Expert
Community Expert
June 8, 2020

Have you done any debug? 

Are any messages displayed? 

Where are Doc1 and Doc2 defined and created?

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often