Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Find latest file in folder & Open

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Les - 25 Mar 2008 13:10 GMT
Hi all, i would like to find the latest file in a folder and open it with
code. The files would have different names as they are updates with a code on
the end. Could somebody help me with this please.

Signature

Les

Tom Hutchins - 25 Mar 2008 15:02 GMT
Try this:

Sub AAAAA()
   Const FilePath = "D:\Data\"
   Workbooks.Open Filename:=FindNewestFile(FilePath)
End Sub

Function FindNewestFile(FilePath As String) As String
Dim LastDate As Date, NewDate As Date
Dim LastFile As String, NewFile As String
'Check all the .XLS files in the folder.  Find the
'most recent file.
LastFile$ = LCase$(Dir(FilePath$ & "*.XLS"))
LastDate = FileDateTime(FilePath$ & LastFile$)
NewFile$ = LastFile$
Do While Len(NewFile$) > 0
   NewFile$ = LCase$(Dir())
   If Len(NewFile$) = 0 Then Exit Do
   NewDate = FileDateTime(FilePath$ & NewFile$)
   If NewDate > LastDate Then
       LastDate = NewDate
       LastFile$ = NewFile$
   End If
Loop
FindNewestFile$ = FilePath$ & LastFile$
End Function

Hope this helps,

Hutch

> Hi all, i would like to find the latest file in a folder and open it with
> code. The files would have different names as they are updates with a code on
> the end. Could somebody help me with this please.
Les - 26 Mar 2008 08:01 GMT
Thanks Tom
Signature

Les

> Try this:
>
[quoted text clipped - 30 lines]
> > code. The files would have different names as they are updates with a code on
> > the end. Could somebody help me with this please.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.