This function receives a full path of a file name, the number of page to start deleting and a number for page counts to be deleted.
If delete action was successful, the function returns "Passed" string, otherwise it shows the error message and returns "Failed".
To Delete pages 2 & 3 :
DeletePages_FromPDF "D:\MyFile.pdf",2,1
Rich (BB code):
Public Function DeletePages_FromPDF(ThisPDF As String, _
FromPage As Integer, _
Optional PageCount_ToDelete As Integer = 1) As String
' =================================================================
' Procedure Name: DeletePages_FromPDF
' Purpose: Delete several pages from a given file
' Procedure Kind: Sub
' Procedure Access: Public
' Parameter ThisPDF (String): The full path to the pdf file
' Parameter FromPage (Integer): start deleting from this page
' Parameter PageCount_ToDelete (Integer): Count of pages to be deleted.
' Omitting = only one page
' 0 = up to the end of pdf
' Returns Passed : Deletion was successful
' Failed : Deletion was failed
' Author: Kitayama
' Date: 2022/05/19
' =================================================================
On Error GoTo ErrorTrap
Dim PDDocSource As Object
Dim DeleteFrom As Integer
Dim DeleteTo As Integer
Dim cnt As Integer
Dim Msg As String
DeletePages_FromPDF = ""
Set PDDocSource = CreateObject("AcroExch.PDDoc")
' Open file
If PDDocSource.Open(ThisPDF) <> True Then
MsgBox "Unable to open the source PDF"
DeletePages_FromPDF = "Error"
GoTo ExitFunction
End If
cnt = PDDocSource.GetNumPages
If cnt > 1 Then
' Calculate Deleting pages
If PageCount_ToDelete = 0 Then PageCount_ToDelete = cnt - FromPage + 1
DeleteFrom = FromPage - 1
DeleteTo = DeleteFrom + (PageCount_ToDelete - 1)
' Start Deleting
If PDDocSource.deletepages(DeleteFrom, DeleteTo) <> True Then
MsgBox "Unable to Delete " & ThisPDF
DeletePages_FromPDF = "Failed"
GoTo ExitFunction
Else
DeletePages_FromPDF = "Passed"
End If
' save the file
If PDDocSource.Save(&H1, ThisPDF) = False Then
MsgBox "Failed to Save Changes to " & ThisPDF
DeletePages_FromPDF = "Save Error"
End If
End If
ExitFunction:
' Application.Echo True
Exit Function
ErrorTrap:
Select Case Err.Number
Case Else
Msg = Err.Description
End Select
MsgBox Msg
Resume ExitFunction
End Function
This function receives a full path of a file name, the number of page to start deleting and a number for page counts to be deleted.
If delete action was successful, the function returns "Passed" string, otherwise it shows the error message and returns "Failed".
To Delete pages 2 & 3 :
DeletePages_FromPDF "D:\MyFile.pdf",1,1
Rich (BB code):
Public Function DeletePages_FromPDF(ThisPDF As String, _
FromPage As Integer, _
Optional PageCount_ToDelete As Integer = 1) As String
' =================================================================
' Procedure Name: DeletePages_FromPDF
' Purpose: Delete several pages from a given file
' Procedure Kind: Sub
' Procedure Access: Public
' Parameter ThisPDF (String): The full path to the pdf file
' Parameter FromPage (Integer): start deleting from this page
' Parameter PageCount_ToDelete (Integer): Count of pages to be deleted.
' Omitting = only one page
' 0 = up to the end of pdf
' Returns Passed : Deletion was successful
' Failed : Deletion was failed
' Author: Kitayama
' Date: 2022/05/19
' =================================================================
On Error GoTo ErrorTrap
Dim PDDocSource As Object
Dim DeleteFrom As Integer
Dim DeleteTo As Integer
Dim cnt As Integer
Dim Msg As String
DeletePages_FromPDF = ""
Set PDDocSource = CreateObject("AcroExch.PDDoc")
' Open file
If PDDocSource.Open(ThisPDF) <> True Then
MsgBox "Unable to open the source PDF"
DeletePages_FromPDF = "Error"
GoTo ExitFunction
End If
cnt = PDDocSource.GetNumPages
If cnt > 1 Then
' Calculate Deleting pages
If PageCount_ToDelete = 0 Then PageCount_ToDelete = cnt - FromPage + 1
DeleteFrom = FromPage - 1
DeleteTo = DeleteFrom + (PageCount_ToDelete - 1)
' Start Deleting
If PDDocSource.deletepages(DeleteFrom, DeleteTo) <> True Then
MsgBox "Unable to Delete " & ThisPDF
DeletePages_FromPDF = "Failed"
GoTo ExitFunction
Else
DeletePages_FromPDF = "Passed"
End If
' save the file
If PDDocSource.Save(&H1, ThisPDF) = False Then
MsgBox "Failed to Save Changes to " & ThisPDF
DeletePages_FromPDF = "Save Error"
End If
End If
ExitFunction:
' Application.Echo True
Exit Function
ErrorTrap:
Select Case Err.Number
Case Else
Msg = Err.Description
End Select
MsgBox Msg
Resume ExitFunction
End Function
Ok, I am doing something wrong. I am trying to call this function using a button on a form, but I am not real sure how to do that. I have tried using call DeletePages_FromPDF
Ok, I am doing something wrong. I am trying to call this function using a button on a form, but I am not real sure how to do that. I have tried using call DeletePages_FromPDF
You're missing the parameters.
You need to pass three parameters to the function.
1- The full path of the your PDF.
2- The start page you want to delete.
3- How many pages you want to delete.
In OnClick event of your button you should write something like this:
DeletePages_FromPDF "D:\MyFile.pdf",2,3
This will delete pages 2-3-4 of your D:\MyFile.pdf
Ok. I have been trying to adjust some things. I have 2 files for a specific button that need to have pages removed. I have been trying to add another location into this code KitaYama, but it give me the error unable to open the source pdf. However if I take and go with the original code it works fine. How would it need to be altered to allow for additional file locations/pdf files?
Ok. I have been trying to adjust some things. I have 2 files for a specific button that need to have pages removed. I have been trying to add another location into this code KitaYama, but it give me the error unable to open the source pdf. However if I take and go with the original code it works fine. How would it need to be altered to allow for additional file locations/pdf files?
As @Gasman suggests, you can call the same function twice, and pass two different PDFs. It will work on them seperately.
If you want to do it on 1 go, then pass two paths, separated but a comma (,)
In the function split the file name and add a loop to work on each file.
Don't forget to close each file before opening the next one. You can't keep one opened and try to open another file. (in fact you can, but you have to create a new object)
I didn't work on messages, returned values and error trap. You can correct them accordingly. You can pass one or any number of files to this function:
Example of use:
DeletePages_FromPDF "D:\File1.pdf,D:\File2.pdf",2,3
This will delete pages 2-3-4 of both D:\File1.pdf & D:\File2.pdf
Code:
Public Function DeletePages_FromPDF(PDFs As String, _
FromPage As Integer, _
Optional PageCount_ToDelete As Integer = 1) As String
' =================================================================
' Procedure Name: DeletePages_FromPDF
' Purpose: Delete several pages from a given file
' Procedure Kind: Sub
' Procedure Access: Public
' Parameter PDFs (String): The full path to one or more pdf files seperatated by ,
' Parameter FromPage (Integer): start deleting from this page
' Parameter PageCount_ToDelete (Integer): Count of pages to be deleted.
' Omitting = only one page
' 0 = up to the end of pdf
' Author: Kitayama
' Date: 2022/10/17
' =================================================================
Dim PDDocSource As Object
Dim DeleteFrom As Integer
Dim DeleteTo As Integer
Dim cnt As Integer
Dim Msg As String
Dim Doc As Variant
Dim Docs As Variant
DeletePages_FromPDF = ""
Set PDDocSource = CreateObject("AcroExch.PDDoc")
Docs = Split(PDFs, ",")
For Each Doc In Docs
' Open file
If PDDocSource.Open(Doc) <> True Then
MsgBox "Unable to open the source PDF:" & Doc
DeletePages_FromPDF = DeletePages_FromPDF & "," & "Error"
GoTo WorkOnNext
End If
cnt = PDDocSource.GetNumPages
If cnt > 1 Then
' Calculate Deleting pages
If PageCount_ToDelete = 0 Then PageCount_ToDelete = cnt - FromPage + 1
DeleteFrom = FromPage - 1
DeleteTo = DeleteFrom + (PageCount_ToDelete - 1)
' Start Deleting
If PDDocSource.deletepages(DeleteFrom, DeleteTo) <> True Then
MsgBox "Unable to Delete " & Doc
DeletePages_FromPDF = DeletePages_FromPDF & "Failed"
Exit Function
End If
' save the file
If PDDocSource.Save(&H1, Doc) = False Then
MsgBox "Failed to Save Changes to " & Doc
DeletePages_FromPDF = "Save Error"
End If
PDDocSource.Close
End If
WorkOnNext:
Next
End Function
Ah, I see how you did that, I was trying to add the documents into the public function instead of the calling as you did. I will adjust and go from there as soon as I can. Thank you both for the help and update.