Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining a path to find text files to run in a macro

Status
Not open for further replies.

sdfreed

Materials
Oct 26, 2003
6
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

 
Replies continue below

Recommended for you

You can use the Dir statement (look for it in the Help file) to see if there are files in the location you specify. Then you can set a flag variable like you suggested. You may need to do some error trapping (On Error statements).
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:
Code:
fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
    MsgBox &quot;Open &quot; & fileToOpen
End If

Cheers,
Joerd

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
The OpenFile dialog, either thru the Application object if it's available per joerd's post, or the Common Control if it's licensed, or using the API, as also posted in thread766-76166.

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?
 
Now that I am at work, this doesn't even work at all know. I saved my emailed attachments to a disk on the a: drive and figured I could get it to work. But I am getting an error 5 message at: .TextFilePlatform = 437
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor