View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.misc
Harlan Grove[_2_] Harlan Grove[_2_] is offline
external usenet poster
 
Posts: 1,231
Default Macro to open most current file in folder

"Jim Cone" wrote...
Sub LatestFileIs()

....
"Tasha" wrote...
Is there a way for a Macro to open the most current file in a
folder? . . . but I am setting up a macro to run each night and
process the text file, but I need to find out how to get it to
open the most current file. Help???


There are some tasks better suited to console tools. This is one.


Sub test()
'change this to an open method call
MsgBox mrf("C:\DLY\PB*.txt")
End Sub


Function mrf(Optional p As String = ".") As String
Dim tfn As String
tfn = Environ("TEMP") & "\~mrf.tmp"
Shell Environ("COMSPEC") & " /c dir """ & p & _
""" /b /a-d /o-d """ & tfn & """"
Open tfn For Input As #1
Line Input #1, mrf
Close #1
Kill tfn
End Function


And depending on how the OP is launching this macro to run at night,
it may be more efficient still to use a batch file to launch Excel
AFTER finding the latest file in the specified directory, e.g.,


@echo off
for /F %%f in ('dir "C:\DLY\PB*.txt" /b /a-d /o-d') do (
start excel "excelfilewithmacros" "%%f"
goto :EOF
)