How to split a PDF into several documents page-by-page (and verca)

blaksnm

Well-known Member
Joined
Dec 15, 2009
Messages
554
Office Version
  1. 365
Platform
  1. Windows
Hi
Hopefully there is a helping hand out there
I want to split a pdf-document as every page become one singel document.
Then I will delete one (or more) of the files (pages) and merge the remaining files (pages) back into a new pdf-document that will not include the removed pages (obviously)

The procedure wil go something like this:
1. I open a XLSM-file with macro where I can select wich (pdf)-file to copy to folder: PDF-FILE (folder contains just this one file, emty folder if not so)
2. The macro will then split this document such as every page become one singel pdf-file named as pagenumber
3. Then I will remove or delete one or more files (pages) - manually or otherwise?)
4. After removing unwanted files in folder I want to merge the remaining files together in pagenr-order as one new file: NEWFILE.PDF

I dont have clue how to to this
Do anybody know?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Install PDFtk Server and run it from VBA to split and merge PDFs.

These threads show how to split and merge PDFs using PDFtk Server:

 
Upvote 0
Install PDFtk Server and run it from VBA to split and merge PDFs.

These threads show how to split and merge PDFs using PDFtk Server:

Thanks - Ill dive into this :)
 
Upvote 0
Thanks for guiding me

The "split-macro" is doing its jobb, but i struggle a bit with my merging-procedure.
After finding this fragment on the net I came almost to the finnish line, but not quite so ....:

Do While file <> vbNullString
p = InStrRev(file, ".")
Select Case LCase(Mid(file, p))
Case ".pdf"
Set Wbk = Workbooks.Open(Filename:=sourcePath & file)
PDFfile = Left(file, p - 1) & "_pdf.pdf"
Wbk.ExportAsFixedFormat Type:=xlTypePDF, Filename:=destPath & PDFfile
Wbk.Close SaveChanges:=False
End Select
file = Dir
Loop

This will convert and merge Excel-files in a folder, but my goal is to merge pdf's only
What kind of ajustment do I need to do here?

The complete macro goes laike this:
Public Sub ExcelSaveAsPDFandMerge()

Dim FD As FileDialog
Dim file As String, p As Long
Dim Wbk As Workbook
Dim sourcePath As String, destPath As String
Dim PDFfile As String

Set FD = Application.FileDialog(msoFileDialogFolderPicker)
With FD
.Title = "Please select the folder contains the Excel files you want to convert:"
.InitialFileName = Range("FilMappe").Value & "\" '"C:\"
If Not .Show Then Exit Sub
sourcePath = .SelectedItems.Item(1) & "\"

.Title = "Please select a destination folder to save the converted files:"
.InitialFileName = Range("FilMappe").Value & "\" '"C:\
If Not .Show Then Exit Sub
destPath = .SelectedItems.Item(1) & "\"
End With

file = Dir(sourcePath & "*.*")

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Do While file <> vbNullString
p = InStrRev(file, ".")
Select Case LCase(Mid(file, p))
Case ".pdf"
Set Wbk = Workbooks.Open(Filename:=sourcePath & file)
PDFfile = Left(file, p - 1) & "_pdf.pdf"
Wbk.ExportAsFixedFormat Type:=xlTypePDF, Filename:=destPath & PDFfile
Wbk.Close SaveChanges:=False
End Select
file = Dir
Loop

Application.DisplayAlerts = True
Application.ScreenUpdating = True

Merge_PDFs destPath

MsgBox "Done"

End Sub
 
Upvote 0
All you need is the Merge_PDFs routine, but modified to loop through the PDFs in the folder in page number order, which means the split macro should save each page of the PDF with the page number appended, e.g. Page1.pdf, Page2.pdf, .etc.

VBA Code:
Private Sub Merge_PDFs(PDFfolder As String)

    Const Q As String = """"

    Dim Wsh As Object 'WshShell
    Dim inputPDFs As String, outputPDF As String
    Dim PDFfile As String
    Dim command As String
    Dim page As Long
    
    If Right(PDFfolder, 1) <> "\" Then PDFfolder = PDFfolder & "\"
    outputPDF = PDFfolder & "All_PDFs_Merged.pdf"
    If Dir(outputPDF) <> vbNullString Then Kill outputPDF
    
    page = 1
    inputPDFs = ""
    Do
        PDFfile = Dir(PDFfolder & "Page" & page & ".pdf")
        If PDFfile <> vbNullString Then inputPDFs = inputPDFs & Q & PDFfile & Q & " "
        page = page + 1
    Loop While PDFfile <> vbNullString
    
    'Merge PDFs with cat

    command = "CD /D " & Q & PDFfolder & Q & " & PDFtk " & inputPDFs & " cat output " & Q & outputPDF & Q
    Set Wsh = CreateObject("WScript.Shell") 'New WshShell
    Wsh.Run "cmd /c " & command, 0, True
    
End Sub
 
Upvote 0
All you need is the Merge_PDFs routine, but modified to loop through the PDFs in the folder in page number order, which means the split macro should save each page of the PDF with the page number appended, e.g. Page1.pdf, Page2.pdf, .etc.

VBA Code:
Private Sub Merge_PDFs(PDFfolder As String)

    Const Q As String = """"

    Dim Wsh As Object 'WshShell
    Dim inputPDFs As String, outputPDF As String
    Dim PDFfile As String
    Dim command As String
    Dim page As Long
   
    If Right(PDFfolder, 1) <> "\" Then PDFfolder = PDFfolder & "\"
    outputPDF = PDFfolder & "All_PDFs_Merged.pdf"
    If Dir(outputPDF) <> vbNullString Then Kill outputPDF
   
    page = 1
    inputPDFs = ""
    Do
        PDFfile = Dir(PDFfolder & "Page" & page & ".pdf")
        If PDFfile <> vbNullString Then inputPDFs = inputPDFs & Q & PDFfile & Q & " "
        page = page + 1
    Loop While PDFfile <> vbNullString
   
    'Merge PDFs with cat

    command = "CD /D " & Q & PDFfolder & Q & " & PDFtk " & inputPDFs & " cat output " & Q & outputPDF & Q
    Set Wsh = CreateObject("WScript.Shell") 'New WshShell
    Wsh.Run "cmd /c " & command, 0, True
   
End Sub
Thanks, but this did som unexpected resault: All pages was presented in an excelfile - not as pdf
I dont know why
 
Upvote 0
Sorry, I don't understand your last reply. My code doesn't read or create any Excel files.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,845
Members
449,051
Latest member
excelquestion515

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