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
Post a Comment