Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Dir function not working on Excel 2010

I mapped an intranet location using the File Explorer. i.e. mapped http://intranet.XXXXXXX.com/mydir/ to M:\

I'm using the Dir function to test if a file is present in that location:

 Dim FileExists as Boolean

 FileExists = Dir("M:\myfile") <> ""

 If FileExists Then MsgBox "File found in M:"

I run that macro on Excel 2007 and it Works Fine. When I run it on Excel 2010 though, Dir("M:\myfile") always returns "", even if the file is present in the specified location. I can´t find a solution that will work on both Excel versions. Any ideas?

like image 369
user3366899 Avatar asked Dec 21 '25 15:12

user3366899


2 Answers

You may add file extension as a wildcard character at the end of filepath. I gave a try in excel 2010 and it worked for me.

  Dim FileExists As Boolean
    FileExists = Dir("D:\myfile" & "*.txt") <> ""

    If FileExists Then MsgBox "File found in M:"
like image 71
Santosh Avatar answered Dec 24 '25 03:12

Santosh


I found that if I use the full network name, it works first go. This wasn't just in VBA, but also some shortcuts also - they returned "File could not be found".

Changing from the mapped shortcut, e.g.

Y:\Projects\Proj1\File1.xlsx

to the full mapped path, e.g.

\\server\Department\Projects\Proj1\File1.xlsx

Fixed the problem

like image 27
Scott Avatar answered Dec 24 '25 05:12

Scott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!