Solved

How can I print the Word Documents in a specified folder in the alpha-numerical order of file names?

Posted on 2015-01-13
2
58 Views
Last Modified: 2015-04-13
I am using the following code to print all of the word documents .docx in a designated folder.
How should I change this code so that it prints in the alpha-numerical order that the files names appear in the folder?
Thanks in advance

Private Sub CommandButton21_Click()
   Dim sMyDir As String
   Dim sDocName As String
   ' The path to obtain the files.
   sMyDir = "\Folder Path"
   sDocName = Dir(sMyDir & "*.DOCX")
   While sDocName <> ""
      ' Print the file.
      Application.PrintOut FileName:=sMyDir & sDocName
      ' Get next file name.
      sDocName = Dir()
   Wend
End Sub

Open in new window

0
Comment
Question by:spar-kle
2 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40548409
Hi,

pls try

Sub PrintFileNameList()

Dim FolderFiles() As Variant 
    smyDir = "\myDir\"
    tmp = Dir(smyDir & "*.docx")
    While tmp <> Empty
        fCount = fCount + 1
        ReDim Preserve FolderFiles(1 To fCount)
        ' declares the array variable again (size+1)
        FolderFiles(fCount) = tmp
        tmp = Dir()
    Wend
    Call BubbleSort(FolderFiles)

    For Each Item In FolderFiles
      Application.PrintOut Filename:=smyDir & sDocName
    Next
End Sub

Sub BubbleSort(MyArray() As Variant)
     
    First = LBound(MyArray)
    Last = UBound(MyArray)
    For i = First To Last - 1
        For j = i + 1 To Last
            If UCase(MyArray(i)) > UCase(MyArray(j)) Then
                Temp = MyArray(j)
                MyArray(j) = MyArray(i)
                MyArray(i) = Temp
            End If
        Next j
    Next i

End Sub

Open in new window

Regards
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 40548814
In Windows Explorer, sort the files into the required order by File Name and then select all of them. Right click on the first one and there should be an option to Print. This should Open, Print and Close the files in the order listed in Explorer.

Thanks
Rob H
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question