Determining a path to find text files to run in a macro
Determining a path to find text files to run in a macro
(OP)
o.k. here I go again.
I am importing text files into my vba macro with the data to be processed. I have everything on a disk (A:\). When I go to a different computer to run the macro, the program doesn't know where to find my text files.
If I show the path by: opening the spreadsheet -> file -> open -> a:\ the macro runs fine. I recorded this and the recorder shows ChDir "A:\" I put that at the begining of the macro and it works great.
The question boils down to; what if I emailed all of the documents and don't know where the user is going to save them? Would I use an if then else statement?
ChDir "A:\"
if filename = "supplier*" + ".txt"
then = true
else chdir "c:\"
if filename = "supplier*" + ".txt"
then = true
else ???? end???
I am just not sure on how to write this, or if I am on the right track. Does this make any sense to anyone out there?
SDFreed
I am importing text files into my vba macro with the data to be processed. I have everything on a disk (A:\). When I go to a different computer to run the macro, the program doesn't know where to find my text files.
If I show the path by: opening the spreadsheet -> file -> open -> a:\ the macro runs fine. I recorded this and the recorder shows ChDir "A:\" I put that at the begining of the macro and it works great.
The question boils down to; what if I emailed all of the documents and don't know where the user is going to save them? Would I use an if then else statement?
ChDir "A:\"
if filename = "supplier*" + ".txt"
then = true
else chdir "c:\"
if filename = "supplier*" + ".txt"
then = true
else ???? end???
I am just not sure on how to write this, or if I am on the right track. Does this make any sense to anyone out there?
SDFreed





RE: Determining a path to find text files to run in a macro
Another way is to open a file/open dialog, so the user can browse for the file. See for example Thread559-55331, or try this from XL help:
fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
MsgBox "Open " & fileToOpen
End If
Cheers,
Joerd
Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
RE: Determining a path to find text files to run in a macro
If you need, you can write a routine to check every directory on every drive looking for the files. Although it may take some time to run, the code is fairly straightforward.
What direction do you think you want to go in?
RE: Determining a path to find text files to run in a macro
and at .TextFileTrailingMinusNumbers = True
If I get rid of those steps it seems to work (for now). Are those process computer specific and that is why I was getting the run time error?
The filetopen = worked great for what I needed. Thank you Joerd