View Single Post
 
Old 07-10-2017, 08:33 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you can access the directory from Windows File Explorer then yes. Call the following function from your macro to write the name of the file in your document. Test it with the test macro and the folder in question.

Code:
Option Explicit

Sub Test()
MsgBox LatestFile("C:\Path") 'The path you want to test
End Sub

Function LatestFile(strPath As String) As String
Dim oFS As Object
Dim strFile As String
Dim strFilename As String
Dim dDate As Date
    Do Until Right(strPath, 1) = Chr(92)
        strPath = strPath & Chr(92)
    Loop
    Set oFS = CreateObject("Scripting.FileSystemObject")
    dDate = "01/01/1900"
    strFile = Dir$(strPath & "*.*")
    While strFile <> ""
        If oFS.GetFile(strPath & strFile).DateLastModified > dDate Then
            strFilename = strPath & strFile
            dDate = oFS.GetFile(strPath & strFile).DateLastModified
        End If
        strFile = Dir$()
    Wend
    LatestFile = strFilename
lbl_Exit:
    Set oFS = Nothing
    Exit Function
err_Handler:
    LatestFile = ""
    Err.Clear
    GoTo lbl_Exit
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote