excel - How Dir() works in VBA -


in current vba code generate variable called myfilename , use parameter when checking if file exists error message "runtime error 52, bad file name or number" in line use dir command.

interestingly if type the filepath dir command manually instead of using myfilename variable works without issue. (no typo, can use myfilename in reading or writing file, drops error dir command)

any ideas how can make dir(myfilename) working?

    set fs = createobject("scripting.filesystemobject")      myfilename = environ("userprofile") & "\application data\myfile.txt"      if dir(myfilename) = ""         set = fs.createtextfile(myfilename, true)         a.write ("0")         a.close     end if 

the \application data not folder shortcut. use localappdata or appdata instead:

myfilename = environ("localappdata") & "\myfile.txt" myfilename = environ("appdata") & "\myfile.txt" 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -