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
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regarding Notepad++ 4 56
Referencing a Master Template for VBA changes 3 63
Word VBA - make this code more efficient 14 37
How do I force Tracked changes 6 43
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

737 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