Open the last file in a folder

G

Guest

I would be very grateful for some code to open the last file in a folder.
They are date stamped but the date might not be known!! So it really has to
be the last one no matter what date it is!
 
J

Jean-Yves

Hi Spike,

This is using Microsoft Scripting Runtime library (fileSystemObject)


Function ShowLastfile(folderspec As String) As String
Dim fso, f, f1, fc
Dim sFileName As String
Dim dDate As Date

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 In fc
If dDate < f1.datecreated Then
dDate = f1.datecreated
sFileName = f1.Name
End If
Next
ShowLastfile = sFileName
End Function

Sub test()
MsgBox ShowLastfile("C:\Temp\")
End Sub

Regards
Jean-Yves
 

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