A few words of thanks would be greatly appreciated.

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

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
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 (ccm.net).