View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.misc
Tasha Tasha is offline
external usenet poster
 
Posts: 157
Default Macro to open most current file in folder

Yes, the error is highlighted on mrf and the window that popped up says
Compile error: Sub or Function not defined.

This is the code I have showing:

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

Also, this will just show which file is the most recent right? I need it to
actually open the most recent file in the import window. I have another
macro set up to process the file from that point, but I need it to actually
get it to the import text file window because this macro is set on a timer at
night when nobody is here, so a display won't help. ??? Can this be done?

"Dave Peterson" wrote:

Did you copy and paste the mrf function?

If you did, you may want to share what you tried and indicate the line that
caused the error. (Harlan's code worked fine for me in my simple test.)

Tasha wrote:

Compile error: Sub or function not defined. What am I doing wrong? I
changed the path to my folder????

"Harlan Grove" wrote:

"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
)



--

Dave Peterson