MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Choose a directory & file


Posted by Michele on February 22, 2001 1:21 AM

At one point in my application, I want to ask the user to choose a directory/path and a file name and I want the user to be able to do this via the standard Windows method / display boxes - just like when opening a file. I don't want the user to open the file but just choose it (then I want to do something with the directory/path & file name choosen). I just can't find the instructions to do this.
Your help would be much appreciated.
Michele


Posted by JAF on February 22, 2001 5:01 AM

Michele

The following should be what you need:
Sub Option1_ALL_FILES()
StrSpecifiedFile = Application.GetOpenFilename(Title:="Please select a file")
If StrSpecifiedFile = False Then
'Actions if user selects a file
MsgBox "The user did not choose a file"
Else
'Actions if user does not select a file
MsgBox "The user chose: " & StrSpecifiedFile
End If
End Sub

If you need to restrict the users to specify files of a specific type the you can use:
Sub Option2_TEXT_FILES()
StrSpecifiedFile = Application.GetOpenFilename(Title:="Please select a file", filefilter:="Text files *.txt (*.txt),")
If StrSpecifiedFile = False Then
'Actions if user selects a file
MsgBox "The user did not choose a file"
Else
'Actions if user does not select a file
MsgBox "The user chose: " & StrSpecifiedFile
End If
End Sub

(obviously specifying the fiel extension required).

Hope this helps.

JAF