Sorting a folder

A

Adrian

Hi,

Can anyone help to write some code to sort into
alphabetical order the contents of one folder?

The files are all word docs with single word filenames. I
want to be able to arrange them, programatically, into
order as they need to be called in strict alphabetical
sequence.

Files will ne added and deleted by the user so a quick bit
of code that I can add to a macro that they will use to
create new files would make life a lot easier.

Thank you for any pointers you can give.

Regards
Adrian
 
P

Peter Hewett

Hi Adrian

You can use the following code to do what you want:

Public Sub BatchUpdate2K()
Dim docToModify As Word.Document
Dim lngindex As Long

' Set the folder to search and type of file
With Application.FileSearch
.NewSearch
.LookIn = "c:\documents\tests\"
.SearchSubFolders = True
.FileName = "*.doc"
.FileType = msoFileTypeWordDocuments
If .Execute(msoSortByFileName) > 0 Then
For lngindex = 1 To .FoundFiles.Count

' File listed in alphabetical Filename order
Debug.Print .FoundFiles(lngindex)

Next lngindex
Else
MsgBox "There are no files is the search path that match the search mask"
End If
End With
End Sub

The above searches for all documents (*.doc) in the folder "c:\documents\tests\" and all
sub-folders.

Just replace the "Debug.Print" with what you want.

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top