If Dir (mydirectory) = "" Then 'The directory does not exist or is not well drafted End If
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 Output: Set Obj = Nothing Exit Function Error: TestSiVide = -1 Resume Output 'The Resume is necessary to avoid issues in the stack. End Function
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" Else MsgBox "The selected directory is valid" End If End If End Sub
DON'T MISS