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
75 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 50

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 33

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

685 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