+ Reply to Thread
Results 1 to 3 of 3

Opening the most recent file saved to a folder

Hybrid View

  1. #1
    Registered User
    Join Date
    06-12-2013
    Location
    Rochester, Minnesota
    MS-Off Ver
    Excel 2007
    Posts
    80

    Opening the most recent file saved to a folder

    Hi Folks,

    I am trying to use the code below to open the most recently saved file in my downloads folder, however it is returning an error that it can't find it. I believe this is because the code is including the file extension ".xls" in the name search.. I am wondering if someone can confirm this and help augment the code so that it drops the file extension from the name?

    Thanks in advance for all posts

    
    Sub GetMostRecentFile()
        
        Dim FileSys As FileSystemObject
        Dim objFile As File
        Dim myFolder
        Dim strFilename As String
        Dim dteFile As Date
            
        'set path for files - change for your folder
        Const myDir As String = "C:\Users\smokybear\Downloads"
        
        'set up filesys objects
        Set FileSys = New FileSystemObject
        Set myFolder = FileSys.GetFolder(myDir)
            
        
        'loop through each file and get date last modified. If largest date then store Filename
        dteFile = DateSerial(1900, 1, 1)
        For Each objFile In myFolder.Files
            If objFile.DateLastModified > dteFile Then
                dteFile = objFile.DateLastModified
                strFilename = objFile.Name
            End If
        Next objFile
        Workbooks.Open strFilename
                
        Set FileSys = Nothing
        Set myFolder = Nothing
    End Sub

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Opening the most recent file saved to a folder

    Hi there,

    Try the following code and see if it does what you need:

    
    
    Sub GetMostRecentFile()
        
    '   Set path for files - change for your folder
        Const MY_DIR    As String = "C:\Users\smokybear\Downloads"
    
        Dim objFileSys  As FileSystemObject
        Dim sFilename   As String
        Dim objFolder   As Folder
        Dim objFile     As File
        Dim dteFile     As Date
            
    '   Set up filesys objects
        Set objFileSys = New FileSystemObject
        Set objFolder = objFileSys.GetFolder(MY_DIR)
        
    '   Loop through each file and get date last modified. If largest date then store Filename
        dteFile = DateSerial(1900, 1, 1)
        For Each objFile In objFolder.Files
    
            If objFile.DateLastModified > dteFile Then
                dteFile = objFile.DateLastModified
                sFilename = objFile.Name
            End If
    
        Next objFile
    
        Workbooks.Open objFolder.Path & "\" & sFilename
                
        Set objFileSys = Nothing
        Set objFolder = Nothing
    
    End Sub
    The most significant change from your original code is in the highlighted line.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Opening the most recent file saved to a folder

    Hi again,

    Many thanks for the Reputation increase

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] File saved w macro not showing up in Recent Documents
    By ILoveStMartin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-29-2013, 09:07 PM
  2. Open a file in the most recent folder
    By primaldeity in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-27-2013, 07:34 PM
  3. Macro For opening Most recent file in folder
    By dtaganovic16 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-13-2012, 12:00 PM
  4. Searching for file by most recent saved time.
    By CJPHX in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-16-2010, 10:31 AM
  5. Searching for most recent file saved.
    By CJPHX in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-31-2010, 03:13 PM
  6. How to open the most recent file in a folder
    By Mr. Structure in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-30-2010, 09:42 AM
  7. How to Open Most Recent File in a Folder
    By 1100shadowman in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-16-2009, 08:30 PM
  8. UDF for opening most recent file added
    By jaden41 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-12-2005, 07:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1