Finding Last Created / Most Recent File with VBA not referencing Microsoft Scripting Runtime (The Answer)

L

Legacy 185660

Guest
Hey everyone! I was looking around for a way to find the last created or last modified file in folder and couldn’t find a way to do it without using the Microsoft Scripting Runtime reference, after a lot of searching to figure out how to reference the dll automatically, I realized that I could do this the same way I send emails through outlook. I found using objects works best and doesn’t require you to manually reference anything, so I experimented a bit with those and it worked perfectly!
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Granted it is probably not as efficient as using the reference, but I would still rather have this then explain to every co-worker what to do<o:p></o:p>

Code:
Sub mostRecentCreatedFile()
 
    'Folder path you want to search
    Dim strFolderPath As String: strFolderPath = "C:\Users\salim.nanji\AppData\Local\Temp"
 
    Dim fileSystem As Object: Set fileSystem = CreateObject("Scripting.FileSystemObject")
 
    Dim folder As Object: Set folder = fileSystem.GetFolder(strFolderPath)
    Dim file As Object
 
    Dim strFilename As String
    Dim fileDate As Date
 
    'Loop through all files in directory
    For Each file In folder.Files
 
        'Find newest file that is also an HTML or XML (Report/Excel)
        'If file.DateCreated > fileDate And (file.Type = "HTML Document" Or file.Type = "XML") Then
 
        'Finds newest file of any type
        If file.DateCreated > fileDate Then
            fileDate = file.DateCreated
            strFilename = file.Name
        End If
    Next file    'Displays last created file name
    MsgBox strFilename
 
End Sub

This code was made to go through my temp folder and gather up some reports, so I used file.Type to make sure I only pulled types I needed.


This can easily be changed to last modified by changing .DateCreated to .DateLastModified

Enjoy!
 
Last edited by a moderator:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
As a funtion (will return file name)

Code:
Function mostRecentFile(strFolderPath As String)
    
    Dim fileSystem As Object: Set fileSystem = CreateObject("Scripting.FileSystemObject")
    Dim folder As Object: Set folder = fileSystem.GetFolder(strFolderPath)
    Dim file As Object
    
    Dim strFileName As String
    Dim fileDate As Date
    
    'Loop through all files in directory
    For Each file In folder.Files
    
        'Finds newest file of any type
        'If file.DateCreated > fileDate Then
        
        'Find newest file that is also an HTML or XML (Report/Excel)
        If file.DateCreated > fileDate And (file.Type = "HTML Document" Or file.Type = "XML") Then
            fileDate = file.DateCreated
            strFileName = file.Name
        End If
    Next file
    'Displays last created file name
    mostRecentFile = strFileName
    
End Function
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,195
Latest member
Stevenciu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top