VBA Working With Files and Directories Without Using FileSystemObject
Determining If Folders and Files Exist
To determine if a file exists, simply pass the filename to the Dir$ function and test to see if it returns a result. Note that Dir$ supports wild-cards, so to test for a specific file, the passed pathName should to be tested to ensure that it does not contain them. The sample below raises an error - if this isn't the desired behavior, the function can be changed to simply return False.
Folders (Dir$ method):
The Dir$() function can also be used to determine if a folder exists by specifying passing vbDirectory for the optional attributes parameter. In this case, the passed pathName value must end with a path separator (\), as matching filenames will cause false positives. Keep in mind that wild-cards are only allowed after the last path separator, so the example function below will throw a run-time error 52 - "Bad file name or number" if the input contains a wild-card. If this isn't the desired behavior, uncomment On Error Resume Next at the top of the function. Also remember that Dir$ supports relative file paths (i.e. ..\Foo\Bar), so results are only guaranteed to be valid as long as the current working directory is not changed.
Folders (ChDir method):
The ChDir statement can also be used to test if a folder exists. Note that this method will temporarily change the environment that VBA is running in, so if that is a consideration, the Dir$ method should be used instead. It does have the advantage of being much less forgiving with its parameter. This method also supports relative file paths, so has the same caveat as the Dir$ method.