Skip to main content
Participant
Sep 14, 2018 7:48 AM UTC
Question

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

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 is closed to new replies. Start a new post to keep the conversation going.

6 replies

Bernd Alheit
Community Expert
Community Expert
Sep 14, 2018 8:38 AM UTC

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

Translate
try67
Community Expert
Community Expert
Sep 14, 2018 9:26 AM UTC

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

Translate
try67
Community Expert
Community Expert
Sep 14, 2018 8:30 AM UTC

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

Translate
Bernd Alheit
Community Expert
Community Expert
Sep 14, 2018 9:57 AM UTC

What can you see at the properties of this documents?

Translate