×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

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:

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

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?

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

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources