Checking For File Existence Loop

anichols

Board Regular
Joined
Mar 11, 2021
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I have a code that is supposed to check if a file is open and then call another sub (which sends an email) It is supposed to loop and check every 5 seconds but it doesn't seem to recognize when the file is in place. I'm sure it's just something simple I'm overlooking.

VBA Code:
Sub Detect_DAR()
    Const MAX_WAIT_SECS As Long = 7200
    Const WAIT_SECS As Long = 5
    Dim hWnd As Long
    Dim ans As VbMsgBoxResult
    Dim endTime As Single
    endTime = Timer + MAX_WAIT_SECS
    Dim strFileName As String
    Dim strFileExists As String
    strFileName = Sheets(2).Range("D16").Value
    strFileExists = Dir(strFileName)
    Do
           If strFileExists <> "" Then
                Call DAR_Comp
                Exit Sub
            End If
        If Timer > endTime Then
            Call DQ_Fail
            Exit Sub
        Else
            PauseMacro WAIT_SECS
        End If
    Loop
Call DQ_Unknown
End Sub

Any help is much appreciated!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I normally use the following function to check if a file is free or busy:
VBA Code:
Function FileStatus(filename As String) As Variant
'Check file status; codice di ritorno:
'0=file is free, 70=file is busy, 53=file not found
'76=path not found
'altri errori: da indagare
'
    Dim filenum As Integer, errnum As Integer
'
    On Error Resume Next   ' Turn error checking off.
    filenum = FreeFile()   ' Get a free file number.
    ' Attempt to open the file and lock it.
    Open filename For Input Lock Read As #filenum
    Close filenum          ' Close the file.
    errnum = Err           ' Save the error number that occurred.
    On Error GoTo 0        ' Turn error checking back on.
FileStatus = errnum
End Function
Then I use
VBA Code:
FStat = FileStatus(FullPathAndNameOfFile)

If FStat=0 then the file is available and free; other values means busy or errors, as described in the comments

Bye
 
Upvote 0
I have a code that is supposed to check if a file is open and then call another sub (which sends an email) It is supposed to loop and check every 5 seconds but it doesn't seem to recognize when the file is in place.
Checking whether a file is open or whether it exists are two different things. Which one do you want?

Here is your routine modified to check whether the file exists within the timeout. Note - according to the logic in your code, DQ_Unknown is never called so I've commented it out.
VBA Code:
#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
#End If

Sub Detect_DAR()

    Const MAX_WAIT_SECS As Long = 7200
    Const WAIT_SECS As Long = 5
    Dim endTime As Date
    Dim strFileName As String
    Dim strFileExists As String
    
    endTime = DateAdd("s", MAX_WAIT_SECS, Now)
    strFileName = Sheets(2).Range("D16").Value
    
    Do
        strFileExists = Dir(strFileName)
        If strFileExists = vbNullString Then
            Sleep WAIT_SECS * 1000
            DoEvents
        End If
    Loop While strFileExists = vbNullString And Now < endTime
    
    If strFileExists = vbNullString Then
        'Not found
        DQ_Fail
    Else
        'Found
        DAR_Comp
    End If
    
    'DQ_Unknown 'never called in original macro

End Sub
 
Upvote 0
Solution
Checking whether a file is open or whether it exists are two different things. Which one do you want?

Here is your routine modified to check whether the file exists within the timeout. Note - according to the logic in your code, DQ_Unknown is never called so I've commented it out.
VBA Code:
#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
#End If

Sub Detect_DAR()

    Const MAX_WAIT_SECS As Long = 7200
    Const WAIT_SECS As Long = 5
    Dim endTime As Date
    Dim strFileName As String
    Dim strFileExists As String
   
    endTime = DateAdd("s", MAX_WAIT_SECS, Now)
    strFileName = Sheets(2).Range("D16").Value
   
    Do
        strFileExists = Dir(strFileName)
        If strFileExists = vbNullString Then
            Sleep WAIT_SECS * 1000
            DoEvents
        End If
    Loop While strFileExists = vbNullString And Now < endTime
   
    If strFileExists = vbNullString Then
        'Not found
        DQ_Fail
    Else
        'Found
        DAR_Comp
    End If
   
    'DQ_Unknown 'never called in original macro

End Sub
Thank you for the reply. The code seems to immediately call the found response, and I'm not sure why.
 
Upvote 0
Your solution works great! When I typed something in, I made an error :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top