vba fso file exists works with mapped drive letter but doesn't work with unc path

3.6k views Asked by At

I've used fso.FileExists(fullfilepath) many times before, but always with mapped drive letters. I have the reference to Microsoft Scripting Runtime.

In my new situation, many people map the same drive letter to different paths, so I want to use the UNC path.

I keep getting If fso.FileExists(fullfilepath) evaluating to false when I use the UNC path.

If I use the mapped drive letter that I personally use, it evaluates to true - the file IS there, and the path name hasn't changed.

I found some posts from people indicating they had problems when a folder in the path had spaces in the name, and the solution there was to wrap the fullfilepath variable in double-quotes, like

fullfilepath = Chr(34) & fullfilepath & Chr(34) 

but that didn't work either.

I even pasted the paths in instead of using a variable.

This works:

fso.FileExists("S:\pathpart\file.csv")

This does not work:

fso.FileExists("\\\networklocation\pathpart\file.csv")

I tried adding the Microsoft Visual Basic for Applications Extensibility 5.3 reference, it didn't make a difference.

The file is not hidden or read-only.

I'm completely stumped.

Edited to add the path, for clarity: \\corp\sites\abc2001\MIS\Company\Sterling Data\Investments\branch_referrals_13JUN15.csv_13062015.csv

2

There are 2 answers

0
CompanionCube On

Ok, it was my own error. I was missing one part of the path, since whenever I navigate to it in Windows Explorer the path shows using my mapped drive - and I missed the folder that it was mapped to when I was copying the path.

The way I discovered it, was that I was thinking I'd have to poll the user's machine to see what mapping they had corresponded to at least the beginning part of the target path - since in order to use the program, they would need to have access to the file it was going to import...

So I went about trying to figure out how to convert a mapped drive letter to a UNC path, then I was going to figure how to loop through all the mapped drives of their machine and compare it to the target path...

If you need to do this, the best explanation and code I found was here: VBA to convert a mapped drive to a UNC path

and when I fed that Function my mapped drive, I got back a result of \\corp\sites\abc2001\loc1shared\ which obviously was the cause of my problems.

1
Bee_Riii On

I'm not able to reproduce that specific error for UNC paths but if the fileexists method is not working for you the Dir method may work for you.

Dir("\\Path\to\File.txt") <> ""