Skip to main content
Participant
September 14, 2018
Question

doc.InsertPages(0, PDDoc, i, 1, ture) always return false and fail to insert target PDF(pdf can be edit, no any protection)

  • September 14, 2018
  • 2 replies
  • 2666 views

For below spltePDF function(from Adobe forum), it works fine for most of PDFs, but for some PDF (pdf is editable, no any protection, no image) can't be inserted by .InsertPages() function and return false, but he the PDDoc2.DeletePages function works welll. who can tell why?

Function SplitPDF(MyPath As String, strFileName As String) As Integer

Dim PDDoc As Acrobat.CAcroPDDoc, newPDF As Acrobat.CAcroPDDoc
Dim PDPage As Acrobat.CAcroPDPage
Dim thePDF As String, PNum As Long
Dim NewName As String
Dim i As Integer
Dim Result As Variant
Dim Filename As String
'...

If VBA.Right(VBA.Trim(MyPath), 1) <> "\" Then
  MyPath = MyPath + "\"
End If

thePDF = MyPath & strFileName

Set PDDoc = CreateObject("AcroExch.pdDoc")

Result = PDDoc.Open(thePDF)
If Not Result Then
   MsgBox "Can't open file: " & Filename
   Exit Function
End If

'...
PNum = PDDoc.GetNumPages

For i = 0 To PNum - 1
    Set newPDF = CreateObject("AcroExch.pdDoc")
    newPDF.Create
    NewName = VBA.Left(strFileName, VBA.Len(strFileName) - 4) & "_SplitPage_" & (i + 1) & "_of_" & PNum & ".pdf"
    'newPDF.InsertPages -1, PDDoc, i, 1, 0
    'startPage, _pdDoc, startPage, numpage, 0
   
  
   If newPDF.InsertPages(0, PDDoc, i, 1, 0) = False Then
  
    Set PDDoc2 = CreateObject("AcroExch.pdDoc")
    Result = PDDoc2.Open(thePDF)
    PDDoc2.DeletePages 0, i - 1
    PDDoc2.DeletePages i + 1, PNum - 1
    PDDoc2.Save 1, MyPath & NewName
    PDDoc2.Close
    Set PDDoc2 = Nothing
   Else
   'PDDoc.Save 1, MyPath & NewName
    newPDF.Save 1, MyPath & NewName
    newPDF.Close
    Set newPDF = Nothing
    Set PDDoc = Nothing
  
   End If
  
Next i

SplitPDF = PNum

End Function

This topic has been closed for replies.

2 replies

Bernd Alheit
Community Expert
Community Expert
September 14, 2018

Why do you post this in the forum for Acrobat Javascript? You don't use Javascript.

try67
Community Expert
Community Expert
September 14, 2018

It is JavaScript, actually. The VBA object can execute JS code.

try67
Community Expert
Community Expert
September 14, 2018

The parameters you're supplying to the method are incorrect. Read its documentation very carefully.

Bernd Alheit
Community Expert
Community Expert
September 14, 2018

What can you see at the properties of this documents?