Copy link to clipboard
Copied
Hello guys,
I am new here. I wrote a simple code which needs to merge 2 PDF files.
Problem:
Question:
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
Copy link to clipboard
Copied
The first argument is not valid. There is no page with offset (0 based) vAnzahl01 in doc01.
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).
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.
Copy link to clipboard
Copied
I'll keep that in my mind.
Thanks
Copy link to clipboard
Copied
Can you insert the pages manually?
Copy link to clipboard
Copied
Yes, but doing it manually takes awful lots of time.
Copy link to clipboard
Copied
It was only a test. The document is not protected.
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.