• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

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

 

TOPICS
Edit and convert PDFs

Views

5.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

LATEST

I'll keep that in my mind.

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

Can you insert the pages manually? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

Thank you for the question.

Before I started testing my short procedure, I made already sure, that PDF-files are not write-protected.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines