Hi all,
I currently have an automatic code that can open up the MOST recent (date modified) file in a folder.
It works great BUT my concern is:
Lets say i set a timer of 10 mins to execute this macro, However the macro takes 15 mins to run and inside that space of 15 mins another 2 files are uploaded FileA and FileB.
This code will then execute FileB next as FileB is deemded as the "most recent file" however FileA is lost in the process.
May I know if there is anything I can add to the code to create some sort of queue system? So that in any unforeseen case of longer waiting time then with my scheduled timer, the code still keeps track of FileA and it will proceed to run the macro on FileA then FileB. I have attached my code below for reference.
Any help is greatly appreciated.
Thanks in advance!
I currently have an automatic code that can open up the MOST recent (date modified) file in a folder.
It works great BUT my concern is:
Lets say i set a timer of 10 mins to execute this macro, However the macro takes 15 mins to run and inside that space of 15 mins another 2 files are uploaded FileA and FileB.
This code will then execute FileB next as FileB is deemded as the "most recent file" however FileA is lost in the process.
May I know if there is anything I can add to the code to create some sort of queue system? So that in any unforeseen case of longer waiting time then with my scheduled timer, the code still keeps track of FileA and it will proceed to run the macro on FileA then FileB. I have attached my code below for reference.
Any help is greatly appreciated.
Thanks in advance!
VBA Code:
Option Explicit
Dim RunTimer As Date
Sub recentFileSpecificFolder()
Dim myFile As String, myRecentFile As String, myMostRecentFile As String
Dim recentDate As Date
Dim myDirectory As String
myDirectory = "C:\Users\Textfiles\"
Dim fileExtension As String
fileExtension = "*.txt"
RunTimer = Now + TimeValue("00:10:00")
Application.OnTime RunTimer, "recentFileSpecificFolder"
If Right(myDirectory, 1) <> "\" Then myDirectory = myDirectory & "\"
myFile = Dir(myDirectory & fileExtension)
If myFile <> "" Then
myRecentFile = myFile
recentDate = FileDateTime(myDirectory & myFile)
Do While myFile <> ""
If FileDateTime(myDirectory & myFile) > recentDate Then
myRecentFile = myFile
recentDate = FileDateTime(myDirectory & myFile)
End If
myFile = Dir
Loop
End If
myMostRecentFile = myRecentFile
Workbooks.Open Filename:=myDirectory & myMostRecentFile
End Sub
Sub StoptheCode()
Application.OnTime RunTimer, "recentFileSpecificFolder", , False
MsgBox "The application has been stopped."
End Sub