VBA to delete Pages in Acrobat PDF

brawnystaff

Board Regular
Joined
Aug 9, 2012
Messages
104
Office Version
  1. 365
I have Excel 2016 and Adobe Acrobat XI installed on my computer. I am trying to create an Excel macro to delete specific pages in an Acrobat PDF. I have the page numbers I want deleted listed in column A, but for some reason I can't get it to work.

Listed below is code that I have so far, but not working. Any ideas? Thx.

Code:
Sub DeletePDFPages()
Dim strSourceFullPath As String, strDestinationFullPath As String
Dim iStartPage As Long, iNumPages As Long
Dim PDDocSource As Object, PDDocTarget As Object
Dim Cell As Range, Rng As Range
Application.ScreenUpdating = False
    Set PDDocSource = CreateObject("AcroExch.PDDoc")
    Set PDDocTarget = CreateObject("AcroExch.PDDoc")


    Set Rng = Selection
   
    strSourceFullPath = Application.GetOpenFilename("PDF Files (*.pdf), *.pdf")
    strDestinationFullPath = StripFilename(strSourceFullPath) & InputBox("Output FileName") & ".pdf"
       
     ' Create a new PDDoc
   If PDDocTarget.Create <> True Then
        MsgBox "Unable to create a new PDF"
        Exit Sub
    End If
    
        ' Open the PDF source file (the file we are going to take pages from)
    If PDDocSource.Open(strSourceFullPath) <> True Then
        MsgBox "Unable to open the source PDF"
        Exit Sub
    End If
        
    For Each Cell In Rng


    ' Set the page range you wish to delete
    ' Don't forget that this is zero based
    iStartPage = Cell - 1
  
    ' Set the number of pages you wish to delete
    iNumPages = 0
       
        ' Insert the pages from the source PDF file to the target PDF
   If PDDocSource.DeletePages(iStartPage, iNumPages) <> True Then
       MsgBox "Unable to Delete"
       Exit Sub
   End If
      
   


    Next Cell
    
        ' Save the new file
    If PDDocSource.Save(&H1, strDestinationFullPath) <> True Then
        MsgBox "Unable to save the pdf"
        Exit Sub
    End If
    
    'Close the PDF files
    PDDocSource.Close
    PDDocTarget.Close
     
    ' Clean up
    Set PDDocSource = Nothing
    Set PDDocTarget = Nothing
    Application.ScreenUpdating = True
   MsgBox "File Saved to " & strDestinationFullPath
End Sub
Function StripFilename(sPathFile As String) As String
'given a full path and file, strip the filename off the end and return the path
Dim filesystem As Object


Set filesystem = CreateObject("Scripting.FilesystemObject")


StripFilename = filesystem.GetParentFolderName(sPathFile) & "\"


Exit Function


End Function
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I don't know if excel has the same functions in PDF manipulation, I know the programme generates PDF look alikes, but the file sizes generated by each programme will be different. That said definitive manipulation of a PDF I suspect has to occur with acrobat
 
Upvote 0
Check with the Acrobat Javascript API. It's likely that you are attempting do something outside of Acrobats security permissions.
 
Last edited:
Upvote 0
1. To delete one page in PDF replace this:
PDDocSource.DeletePages(iStartPage, iNumPages)
by that:
PDDocSource.DeletePages(iStartPage, iStartPage)
because the 2nd parameter is the end page number for deleting.

2. The page numbers to be deleted should be in descend order in the selection.
 
Upvote 0

Forum statistics

Threads
1,216,090
Messages
6,128,765
Members
449,467
Latest member
sdafasfasdf

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top