Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 157
Default Macro to open most current file in folder

Is there a way for a Macro to open the most current file in a folder? I have
a file copied to a specified folder nightly (C:\DLY\PB061807.txt) for
example. The PB stays the same in every file, and then it adds the date to
the end. There may be several in the folder(for example over the weekend, on
Monday there will be three files), 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???
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,290
Default Macro to open most current file in folder


Sub LatestFileIs()
'Jim Cone - San Francisco, USA - June 2005
'Displays the latest file name in the strPath folder.

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strPath As String
Dim strName As String
Dim dteDate As Date

' Specify the folder...
strPath = "C:\Program Files\Microsoft Office\Office\Library"
' Use Microsoft Scripting runtime.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)

' Check date on each file in folder.
For Each objFile In objFolder.Files
If objFile.DateLastModified dteDate Then
dteDate = objFile.DateLastModified
strName = objFile.Name
End If
Next 'objFile

' Display file name in message box.
MsgBox strName & " - is latest file - " & dteDate

Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
End Sub
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware




"Tasha"
wrote in message
Is there a way for a Macro to open the most current file in a folder? I have
a file copied to a specified folder nightly (C:\DLY\PB061807.txt) for
example. The PB stays the same in every file, and then it adds the date to
the end. There may be several in the folder(for example over the weekend, on
Monday there will be three files), 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???
  #3   Report Post  
Posted to microsoft.public.excel.misc
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
)

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 157
Default Macro to open most current file in folder

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
)


  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Macro to open most current file in folder

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


  #6   Report Post  
Posted to microsoft.public.excel.misc
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

  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Macro to open most current file in folder

You didn't copy|paste this portion:

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


(copy from Harlan's original post to avoid those 's)

Tasha wrote:

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


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word and Excel crash when I try to open a folder to get to a file DGD41 Excel Discussion (Misc queries) 7 April 15th 06 09:14 PM
Unable to open a file from its icon in folder John Excel Discussion (Misc queries) 4 February 14th 06 02:02 AM
Macro syntax to open file in current explorer folder [email protected] Excel Discussion (Misc queries) 4 January 11th 06 12:07 PM
I can't open an xls.file compressed(zipped)folder in Windows XP? Kadi Excel Discussion (Misc queries) 0 November 16th 05 04:51 PM
Excel should open documents in the folder of the current file boxfactory Setting up and Configuration of Excel 0 October 20th 05 03:35 PM


All times are GMT +1. The time now is 09:32 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"