Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2017, 04:49 AM
spider85 spider85 is offline Get the latest filename from directory with a macro Windows 10 Get the latest filename from directory with a macro Office 2016
Novice
Get the latest filename from directory with a macro
 
Join Date: Apr 2017
Posts: 4
spider85 is on a distinguished road
Default Get the latest filename from directory with a macro

Hi,



Is there a possibility to make a macro to insert the filename of the latest picture from a camera roll directory?


So when i take a picture i can input the filename of that picture in a word document.


Kind Regards
Reply With Quote
  #2  
Old 07-10-2017, 08:33 PM
gmayor's Avatar
gmayor gmayor is offline Get the latest filename from directory with a macro Windows 10 Get the latest filename from directory with a macro Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 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
  #3  
Old 07-10-2017, 10:43 PM
spider85 spider85 is offline Get the latest filename from directory with a macro Windows 10 Get the latest filename from directory with a macro Office 2016
Novice
Get the latest filename from directory with a macro
 
Join Date: Apr 2017
Posts: 4
spider85 is on a distinguished road
Default

Works, tnx!!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the latest filename from directory with a macro Mailmerge Directory - TableJoiner Macro Gwinnett Mail Merge 3 03-01-2017 09:25 AM
Get the latest filename from directory with a macro Problem getting filename and directory of current word doc. gazeranco Word VBA 8 10-29-2016 12:13 AM
Macro Winword mailmerge split printpage with mergefield as filename pca Word VBA 2 02-28-2014 02:43 PM
Get the latest filename from directory with a macro Word Macro saving with different filename terraterm Word VBA 5 02-28-2014 12:02 PM
Macro for automatically adding filename without .doc RPM7 Word VBA 0 04-29-2010 01:43 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:19 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft