VBA VB6 - When the test DIR (directory) goes down

The simplest test to check out if the path of a directory is valid is of course ...

 If Dir (mydirectory) = "" Then 
        'The directory does not exist or is not well drafted 
    End If 

Exception ... If the directory path is correctly stared but empty ", but sometimes it may be necessary to determine if a directory exists, even if it is empty.
In such circumstances this test (courrtesy of lermite222)..may be useful

In a general module

Public Function TestSiVide(Rep As String) As Long 
Dim Obj, RepP, F 
    On Error GoTo Error 'If directory is not found 
    Set Obj = CreateObject("Scripting.FileSystemObject") 
    Set RepP = Obj.Getfolder(Rep) 
    Set F = RepP.Files 
    TestSiVide = F.Count 
    Set RepP = Nothing 
    Set F = Nothing 
    Set Obj = Nothing 
    Exit Function 
    TestSiVide = -1 
    Resume Output 'The Resume is necessary to avoid issues in the stack. 
End Function

And in the body of the code ...

Sub Test(Rep as String) 
Dim Nb As Long, MyDirectory as String 
    If Rep <> "" Then 
        MyDirectory = Rep & IIf(Right(Rep, 1) <> "\", "\", "")  
        Nb = TestSiVide(MonRepertoir ) 'Return -1 if error directory (error 76) 
        If Nb = 0 Then 
            MsgBox "The directory selected does not contain a file", vbCritical, "Select directory" 
        ElseIf Nb = -1 Then 
            MsgBox "The selected directory is not/ no longer valid", vbCritical, "Select directory" 
             MsgBox "The selected directory is valid" 
        End If 
    End If 
End Sub
Published by jak58. Latest update on June 1, 2010 at 07:32 AM by jak58.
This document, titled "VBA VB6 - When the test DIR (directory) goes down," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (https://ccm.net/).
Excel - Calculation on excel sheet
Excel/VBA - Get the row number of the first blank cell