Public Function FileExists(Filename As String, _
Optional TreatDirAsFile As Boolean = False) As Boolean
' Returns true if Filename exists. If Filename is a directory and
' TreatDirAsFile is true then the function will also return true.
' Similarly if the file is really a directory and TreatDirAsFile is false
' then the function will return false.
On Error Goto FileIOError ' catch a 404
Dim Attributes As Integer
Attributes = GetAttr(Filename) ' will throw an error if it doesn't exist
If Attributes = vbDirectory And Not TreatDirAsFile Then
FileExists = False ' it's a directory but that's not cool
Else
FileExists = True ' it's a directory but we said that's cool
End If
Exit Function
FileIOError:
FileExists = False ' error thrown at GetAttr, file doesn't exist
End Function