tags:

views:

42

answers:

3

Is there a way to get a sorted list of file names of a folder in VBA? Up to now, I arrived at

Dim fso As Object
Dim objFolder As Object
Dim objFileList As Object
Dim vFile As Variant
Dim sFolder As String

sFolder = "C:\Docs"

Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(sFolder)
Set objFileList = objFolder.Files

For Each vFile In objFileList
    ' do something '
Next vFile

but it is crucial to be sure the processing order of the for loop is determined by the file names...

Any help appreciated!

+2  A: 

Download a VBA sort routine such as this one: Recursive Quick Sort

Fill an array with the file names and simply sort as shown here.

Mitch Wheat
+1  A: 

Looks like you can do it by using an ADODB.RecordSet. It's a bit heavy duty, but here's a reference which should get you started.

dcp
+1  A: 

The order is arbitrary.

When you walk over objFileList add the files to an array, then sort the array.

Alex K.