This question has been asked many times in this forum. Here are 2 easy ways to determine whether a file exists or not:
' Usage for methods 1 and 2
Private Sub Command1_Click()
MsgBox FileExistsDIR("C:\Test.txt")
MsgBox FileExistsFSO("C:\Test.txt")
End Sub
' Method 1
Function FileExistsDIR(sFile As String) As Boolean
FileExistsDIR = True
If Dir$(sFile) = vbNullString Then FileExistsDIR = False
End Function
' Method 2
Function FileExistsFSO(sFile As String) As Boolean
Dim fs As Object
FileExistsFSO = True
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(sFile) = False Then FileExistsFSO = False
Set fs = Nothing
End Function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.