The VBA Macro determines if a file or directory exists.
Dim DetermineFileExists As Integer
Dim PathOfName As String
Range("C8").Value = ""
Range("C8").Interior.ColorIndex = 0
' If the file does not exist the program will log an error
On Error Resume Next
PathOfName = Range("C5").Value + "\" + Range("C6").Value
DetermineFileExists = GetAttr(PathOfName)
Select Case Err.Number
Case Is = 0
Range("C8").Value = "The File or Directory Exists"
Range("C8").Interior.ColorIndex = 4
Case Else
Range("C8").Value = "The File or Directory does NOT Exists"
Range("C8").Interior.ColorIndex = 3
End Select
On Error GoTo 0
End Sub
Explanation
To be able to determine if a file or directory exists can be useful in some coding situations. This code is a straight forward approach that logs if a error occurs when trying to use the GetAttr() if a error occurs then the file or directory clearly does not exist. This function is useful when you want to communicate or write to a certain file and you are uncertain if the file really exists and if you refer the file you might crash the program, thus check if the file exist if it does communicate with the file or terminate the program.Code
Public Sub DetermineIfFileDirectoryExists()Dim DetermineFileExists As Integer
Dim PathOfName As String
Range("C8").Value = ""
Range("C8").Interior.ColorIndex = 0
' If the file does not exist the program will log an error
On Error Resume Next
PathOfName = Range("C5").Value + "\" + Range("C6").Value
DetermineFileExists = GetAttr(PathOfName)
Select Case Err.Number
Case Is = 0
Range("C8").Value = "The File or Directory Exists"
Range("C8").Interior.ColorIndex = 4
Case Else
Range("C8").Value = "The File or Directory does NOT Exists"
Range("C8").Interior.ColorIndex = 3
End Select
On Error GoTo 0
End Sub
No comments:
Post a Comment