ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Open Text based on creation date (https://www.excelbanter.com/excel-programming/394565-open-text-based-creation-date.html)

[email protected]

Open Text based on creation date
 
Is there a way to open a text file based on the date it was created.
When I run my macro I want it to open the most recent text file in a
specific folder. currently I'm opening my text through a user command
pop-up window. Thanks

UF = Application.GetOpenFilename(FileFilter:=".TXT
Files(*.txt),*.txt", Title:="Satlog Measured")

Workbooks.Open Filename:=UF, Format:=2


[email protected]

Open Text based on creation date
 
On Aug 1, 11:50 am, wrote:
Is there a way to open a text file based on the date it was created.
When I run my macro I want it to open the most recent text file in a
specific folder. currently I'm opening my text through a user command
pop-up window. Thanks

UF = Application.GetOpenFilename(FileFilter:=".TXT
Files(*.txt),*.txt", Title:="Satlog Measured")

Workbooks.Open Filename:=UF, Format:=2


Can anyone help?


Steve Yandl

Open Text based on creation date
 
This will give you the path and file name to the most recently modified txt
file in "C:\Test"

____________________________________________


Sub FindNewestTextFile()
strFolderPath = "C:\Test"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolderPath)

dtmLatestDate = 0
Set colFiles = objFolder.Files

For Each objFile In colFiles
If objFSO.GetExtensionName(objFile) = "txt" Then
If objFile.DateLastModified dtmLatestDate Then
dtmLatestDate = objFile.DateLastModified
strLatestFile = objFile.Path
End If
End If
Next objFile

If Not dtmLatestDate = 0 Then
MsgBox strLatestFile
End If

Set objFolder = Nothing
Set objFSO = Nothing
End Sub

__________________________________________________

Steve Yandl
wrote in message
oups.com...
On Aug 1, 11:50 am, wrote:
Is there a way to open a text file based on the date it was created.
When I run my macro I want it to open the most recent text file in a
specific folder. currently I'm opening my text through a user command
pop-up window. Thanks

UF = Application.GetOpenFilename(FileFilter:=".TXT
Files(*.txt),*.txt", Title:="Satlog Measured")

Workbooks.Open Filename:=UF, Format:=2


Can anyone help?




[email protected]

Open Text based on creation date
 
On Aug 1, 3:19 pm, "Steve Yandl" wrote:
This will give you the path and file name to the most recently modified txt
file in "C:\Test"

____________________________________________

Sub FindNewestTextFile()
strFolderPath = "C:\Test"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolderPath)

dtmLatestDate = 0
Set colFiles = objFolder.Files

For Each objFile In colFiles
If objFSO.GetExtensionName(objFile) = "txt" Then
If objFile.DateLastModified dtmLatestDate Then
dtmLatestDate = objFile.DateLastModified
strLatestFile = objFile.Path
End If
End If
Next objFile

If Not dtmLatestDate = 0 Then
MsgBox strLatestFile
End If

Set objFolder = Nothing
Set objFSO = Nothing
End Sub

__________________________________________________

Steve wrote in message

oups.com...



On Aug 1, 11:50 am, wrote:
Is there a way to open a text file based on the date it was created.
When I run my macro I want it to open the most recent text file in a
specific folder. currently I'm opening my text through a user command
pop-up window. Thanks


UF = Application.GetOpenFilename(FileFilter:=".TXT
Files(*.txt),*.txt", Title:="Satlog Measured")


Workbooks.Open Filename:=UF, Format:=2


Can anyone help?- Hide quoted text -


- Show quoted text -


What is all of the Dims I tried making most of them as objects but
they wont work still


Steve Yandl

Open Text based on creation date
 
Carlos,

If you first go to 'Tools References' and set a reference to 'Microsoft
Scripting Runtime', you can dim objFSO as "Scripting.FileSystemObject" and
the folder as "Scripting.Folder".

You can also get away with just what I gave you but change the string at the
top to the folder you're actually wanting to check and rather than
generating a message box you would want to feed the file name string to
whatever code you have in your VBA subroutine.

Steve




All times are GMT +1. The time now is 05:34 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com