VBA VB6 - When the test DIR (directory) goes down
On April 21,2022 by Tom RoutleyThe 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 lermite222ay 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 Output: Set Obj = Nothing Exit Function Error: 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" Else MsgBox "The selected directory is valid" End If End If End Sub
Article Recommendations
Latest articles
Popular Articles
Archives
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- February 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- January 2021
Leave a Reply