I have a temp directory that I am appending pdf files to a master file that need to go in a specific order. I save the files to end with what will be the page number in the merged file. The number of files can vary but would not be longer than 2 digits (i.e. 0 > and < 100).
The problem is that the files are appended alphanumerically and not strictly numerically.
TempFile1.pdf
TempFile2.pdf
TempFile3pdf
...
TempFile11.pdf
TempFile12.pdf
...
Gets appended as:
TempFile1.pdf
TempFile11.pdf
TempFile12pdf
...
TempFile2.pdf
TempFile3.pdf
I want it append TempFile1,TempFile2 ,TempFile3 TempFile4, TempFile5 etc.
How can I alter my code below to accomplish this?
Code:
Sub AppendPDF()
Dim DocPath As String
Const strPattern As String = "*.pdf"
Dim strFile As String
Dim Counter As Integer
DocPath = "C:\TempDoc\"
Counter = 1
Dim numPages As Integer
Set AcroApp = CreateObject("AcroExch.App")
Set FirstDoc = CreateObject("AcroExch.PDDoc")
' Search for files that end with a number
strFile = Dir(DocPath & strPattern, vbNormal)
FirstDoc.Open DocPath & strFile
Do While Len(strFile) > 0
LResult = Right(strFile, 1)
strFile = Dir
If Len(strFile) > 0 Then
Set MergeDoc = CreateObject("AcroExch.PDDoc")
iLastPage = FirstDoc.GetNumPages 'length of existing file
MergeDoc.Open DocPath & strFile
FirstDoc.InsertPages iLastPage - 1, MergeDoc, 0, MergeDoc.GetNumPages, True
MergeDoc.Close
Debug.Print "results " & strFile
Set MergeDoc = Nothing
End If
Loop
mergedFile = DocPath & "Merged.pdf"
Debug.Print mergedFile
FirstDoc.Save PDSaveFull, mergedFile
FirstDoc.Close
AcroApp.Exit
OpenFile mergedFile
End Sub
A thousand thanks in advance