This CheckIfFileExists is a simple but very useful function when reading or writing files to the Operating System using VBA. Call this function first before reading a file or writing a file. If reading a file, call this function to check if that exists before trying to read it. If writing a file, call this function to check if the file already exists. If the file exists, you may want to warn the user whether to overwrite the existing file or abort the operation. This is a really useful function and works in all Office Products – Excel, Word, Access!
Function CheckIfFileExists(strFilename As String) As Boolean
'Purpose: This function returns true if the file, given in strFilename, exists - if the file does not exist, then
' the function returns false.
Dim strFileExists As String
strFileExists = Dir(strFilename)
If strFileExists = "" Then
'MsgBox "The selected file doesn't exist"
CheckIfFileExists = False
Else
'MsgBox "The selected file exists"
CheckIfFileExists = True
End If
End Function
This is a complimentary article written by the MAARS team for the MAARS user community. Code in this article drives the operation of MAARS (MS Access Application wRiting Software). MAARS is an intelligent automation program that speeds up MS Access Application Development by 10x, 20x or 100x times. To learn more about MAARS, click here.
Disclaimer:
Some information included in this article may have been sourced from other publicly available websites and blogs. In such cases, credit goes to those authors for the original ideas and thoughts, but we do take credit for putting valuable information together and improve the efficiency of other office developers.