Skip to main content
Known Participant
April 7, 2021
Question

Function InsertPages() [from "AcroExch.PDDoc"] does not insert pages in VBA.

  • April 7, 2021
  • 2 replies
  • 7588 views

Hello guys,

 

I am new here. I wrote a simple code which needs to merge 2 PDF files.

Problem:

  • The fucntion InsertPages() does not want to insert pages from another PDF file. 
  • I am not getting any errors.
  • I am always getting FALSE returned from InserPages().

 

Question:

  • Does anybody have an idea?

 

As you can see, the code is super simple.

 

Sub mergePDFfiles()


    Dim Aapp As Acrobat.AcroApp
    Dim Doc01 As Acrobat.AcroPDDoc
    Dim Doc02 As Acrobat.AcroPDDoc
    Dim Doc03 As Acrobat.AcroPDDoc
    Set Aapp = CreateObject("AcroExch.App")
    Set Doc01 = CreateObject("AcroExch.PDDoc")
    Set Doc02 = CreateObject("AcroExch.PDDoc")
    Set Doc03 = CreateObject("AcroExch.PDDoc")
    
    Aapp.Show
    '------------------------
    Dim sName00, sName01, sName02, sName03 As String
    Dim sPfad00, sPfad01, sPfad02, sPfad03 As String
    Dim vAnzahl01, vAnzahl02, vAnzahl03 As Long
    sPfad00 = ThisWorkbook.Path + "\DocTOTAL.pdf"
    sPfad01 = ThisWorkbook.Path + "\Doc_01.pdf"
    sPfad02 = ThisWorkbook.Path + "\Doc_02.pdf"
    sPfad03 = ThisWorkbook.Path + "\Doc_03.pdf"
    If Doc01.Open(sPfad01) Then
        sName01 = Doc01.GetFileName()
        vAnzahl01 = Doc01.GetNumPages()
        MsgBox sName01 + " has " + CStr(vAnzahl01) + " pages and is open."
    End If
    If Doc02.Open(sPfad02) Then
        sName02 = Doc02.GetFileName()
        vAnzahl02 = Doc02.GetNumPages()
        MsgBox sName02 + " has " + CStr(vAnzahl02) + " pages and is open."
    End If
    If Doc03.Open(sPfad03) Then
        sName03 = Doc03.GetFileName()
        vAnzahl03 = Doc03.GetNumPages()
        MsgBox sName03 + " has " + CStr(vAnzahl03) + " pages and is open."
    End If
    
    If Doc01.InsertPages(vAnzahl01, Doc02, 0, vAnzahl02, 0) = False Then
        MsgBox "Document not inserted"

    Else
        MsgBox "Document inserted"
    End If
    
    If Doc01.Save(PDSaveFull, sPfad00) = False Then
        MsgBox "Document not saved"
    Else
        MsgBox "Document saved under " + sPfad00
    End If
    
    '------------------------
    Doc01.Close
    Doc02.Close
    Doc03.Close
    Aapp.Exit
    Set Aapp = Nothing
    Set Doc03 = Nothing
    Set Doc02 = Nothing
    Set Doc01 = Nothing
End Sub

 

This topic has been closed for replies.

2 replies

Bernd Alheit
Community Expert
Community Expert
April 7, 2021

Can you insert the pages manually? 

SnailMTUAuthor
Known Participant
April 8, 2021

Yes, but doing it manually takes awful lots of time.

Bernd Alheit
Community Expert
Community Expert
April 8, 2021

It was only a test. The document is not protected.

Legend
April 7, 2021

The first argument is not valid. There is no page with offset (0 based)  vAnzahl01 in doc01.

SnailMTUAuthor
Known Participant
April 8, 2021
If Doc01.InsertPages(vAnzahl01 - 1 , Doc02, 0, vAnzahl02, 0) = False Then
        MsgBox "Document not inserted"    
Else
        MsgBox "Document inserted"
End If

Now it works.

Thank you.

Although, this is really not intuitive. I thought, vAnzahl needs to be either 1 or 2, in order for Doc02 to be inserted after Doc01 (in my case Doc01 has only 1 page).

Legend
April 8, 2021

Almost everywhere you will find pages are numbered in the API from zero. So you have to subtract 1 very often.