I have code that goes online grabs all the files in a location. I then convert them into individual PDFs
I then have the code below to combine them into one file
My problem is that some of the PDFs are fillable and have the same form names in them. My two choices are either:
1. Flatten each PDF prior to combining.
2. Add then into one PDF as a portfoliofile
Here is the combining code where I get my errors on some files.
Can anyone lead me in how to first flatten these PDFs or to arrange them into a portfolio?
I then have the code below to combine them into one file
My problem is that some of the PDFs are fillable and have the same form names in them. My two choices are either:
1. Flatten each PDF prior to combining.
2. Add then into one PDF as a portfoliofile
Here is the combining code where I get my errors on some files.
VBA Code:
Sub CombFilesFromWorksheetnames()
setprintarea
'SaveWorksheet as PDF
strName = "TOC"
strPathFile = DL_Files_Dir & strName & ".pdf"
'export to PDF in current folder
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=strPathFile
'---combine and delete pdfs---'
'DirLocation = ThisWorkbook.Path & "\"
lRow = Cells(Rows.Count, 3).End(xlUp).Row
Dim objFSO As Object, objFolder As Object, objFile As Object
Dim strFolderPath As String
Dim strFilePaths As String
' Use the Adobe Acrobat Application to combine the PDF files
Dim AcroApp As Object, PartDocs As Object, AVDoc As Object
Dim newdoc As Object, i As Integer
' Create a new instance of Adobe Acrobat
'Set AcroApp = CreateObject("AcroExch.App")
Set PartDocs = CreateObject("AcroExch.PDDoc")
Set newdoc = Nothing
' Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Get the folder object
Set objFolder = objFSO.GetFolder(DL_Files_Dir)
'start PDF with TOC
' Set newdoc = CreateObject("AcroExch.PDDoc")
' newdoc.Open OutPut_Dir
' newdoc.Save 1, OutPut_Dir & "\" & "TOC.pdf"
Set newdoc = CreateObject("AcroExch.PDDoc")
newdoc.Open DL_Files_Dir & "TOC.pdf"
newdoc.Save 1, OutPut_Dir & "\" & CaseFileName & ".pdf"
For Each cell In Range("D4:D" & lRow)
If cell.Value <> "" Then
pos = InStrRev(cell.Value, ".")
' MsgBox Left(Cell.Value, pos)
fn = Left(cell.Value, pos)
PartDocs.Open DL_Files_Dir & fn & "pdf"
If Not newdoc.InsertPages(newdoc.GetNumPages - 1, PartDocs, 0, PartDocs.GetNumPages, 0) Then
' MsgBox "Error merging " & cell.Value, vbExclamation
MsgBox cell.Value & vbCrLf & vbCrLf & "This file type is not supported. You will need to convert it to a PDF manually if you want it added to the Case File"
End If
PartDocs.Close
End If
Next
' get filename and find in folder
For Each cell In Range("D4:D" & lRow)
If cell.Value <> "" Then
' Loop through each file in the folder
For Each objFile In objFolder.Files
' Check if the file is a PDF file
pos1 = InStrRev(objFile.Name, ".")
pos2 = InStrRev(cell.Value, ".")
If Left(objFile.Name, pos1) = Left(cell.Value, pos2) Then
If LCase(objFile.Name) Like LCase("*" & ".pdf") Then
If newdoc Is Nothing Then
Set newdoc = CreateObject("AcroExch.PDDoc")
newdoc.Open objFile.Path
newdoc.Save 1, OutPut_Dir & "\" & CaseFileName & ".pdf"
Else
PartDocs.Open objFile.Path
If Not newdoc.InsertPages(newdoc.GetNumPages - 1, PartDocs, 0, PartDocs.GetNumPages, 0) Then
MsgBox "Error merging " & objFile.Path, vbExclamation
End If
PartDocs.Close
End If
objFile.Delete
End If
End If
Next objFile
End If
Next
' Save the final combined PDF document
newdoc.Save 1, OutPut_Dir & "\" & CaseFileName & ".pdf"
' Close the merged PDF file
newdoc.Close
End Sub
Can anyone lead me in how to first flatten these PDFs or to arrange them into a portfolio?