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
49 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 48

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 31

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now