Determine If File or Directory Exists
The VBA Macro determines if a file or directory exists.
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.
The entire VBA Excel program is available for downloading at the bottom of this page, enjoy!
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
Range("C8").Value = "The File or Directory does NOT Exists"
Range("C8").Interior.ColorIndex = 3
On Error GoTo 0
Download excel file! Determine_If_File_Or_Directory_Exists.xls
It was simply superb.. The above code has solved my problem
Comment made by: Rameez , 2013-01-03 18:05:25
Comment made by: asda , 2014-08-26 08:05:29