delete specific pages in pdf (1 Viewer)

donkey9972

Registered User.
Local time
Today, 08:37
Joined
May 18, 2008
Messages
30
Hi,

New issue, is there a way to remove the second page of a pdf document using vba?
 

donkey9972

Registered User.
Local time
Today, 08:37
Joined
May 18, 2008
Messages
30
Thank you arnelgp. I came across that post in my searching, but Will it work for access vba?
 

KitaYama

Well-known member
Local time
Tomorrow, 00:37
Joined
Jan 6, 2022
Messages
1,541
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
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 08:37
Joined
Oct 29, 2018
Messages
21,478
Hi,

New issue, is there a way to remove the second page of a pdf document using vba?
Quick question: Do you have the full version of Adobe Acrobat installed? If users will be doing this, would they have the full version of Acrobat too?
 

donkey9972

Registered User.
Local time
Today, 08:37
Joined
May 18, 2008
Messages
30
Well the version. Everyone has access to for Adobe is Adobe acrobat pro dc
 

donkey9972

Registered User.
Local time
Today, 08:37
Joined
May 18, 2008
Messages
30
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
I will try this and ty for the code. I will update as soon as I can.
 

donkey9972

Registered User.
Local time
Today, 08:37
Joined
May 18, 2008
Messages
30
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

I have tried using DeletePages_FromPDF ()

And a few others. What am I missing?
 

KitaYama

Well-known member
Local time
Tomorrow, 00:37
Joined
Jan 6, 2022
Messages
1,541
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

I have tried using DeletePages_FromPDF ()

And a few others. What am I missing?
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
 

donkey9972

Registered User.
Local time
Today, 08:37
Joined
May 18, 2008
Messages
30
Ah, I never had a handle on how to do the functions before. As soon as I can I will implement and update. Again ty.
 

donkey9972

Registered User.
Local time
Today, 08:37
Joined
May 18, 2008
Messages
30
Okay, I was able to set this up and it worked perfectly, ty.
 

donkey9972

Registered User.
Local time
Today, 08:37
Joined
May 18, 2008
Messages
30
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?
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:37
Joined
Sep 21, 2011
Messages
14,323
You call it as many times as you have files to edit. :(
 

KitaYama

Well-known member
Local time
Tomorrow, 00:37
Joined
Jan 6, 2022
Messages
1,541
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
 

donkey9972

Registered User.
Local time
Today, 08:37
Joined
May 18, 2008
Messages
30
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.
 

Users who are viewing this thread

Top Bottom