brawnystaff
Board Regular
- Joined
- Aug 9, 2012
- Messages
- 104
- Office Version
- 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.
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: