Results 1 to 3 of 3
  1. #1
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    111

    Loop through folder, append files ending with number in numeric order

    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

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Need placeholder zeros. So "ABCD2" will sort before "ABCD12", format the names like "ABCD00002" and "ABCD00012". How many zeros are required depends on how far you think a series will go.

    So let strX represent the page number 2:

    "ABCD" & String(5 - Len(strX), "0") & strX

    Returns: ABCD00002
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    111
    For some reason your code did not work but this did:

    Code:
    pdfName = "TempFile" & Format(cnt, "00") & ".pdf" (code occured in another function (WordToPdf) not posted
    Thanks for responding

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 03-07-2016, 05:02 PM
  2. Replies: 10
    Last Post: 09-09-2015, 03:25 AM
  3. Replies: 1
    Last Post: 05-15-2015, 10:58 AM
  4. Number of files in a folder.
    By Iain in forum Reports
    Replies: 3
    Last Post: 06-17-2012, 01:53 PM
  5. order by - string and numeric
    By pen in forum Queries
    Replies: 10
    Last Post: 05-20-2009, 06:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums