Selecting a Directory

N

Nigel

Hi All
On a userform I have a textbox control in which I let the user store the
destination directory for the file(s) created. I have been using
Application.GetOpenFilename to let the user go and find the directory and
choose a file - I then strip the filename leaving the path only and put this
in the control.

It is not elegant since the user has to choose a file to open (anyone will
do) so I can extract the path, problem #1 is that user is now being asked to
open a file which doesn't actually happen just so I can get the directory -
all a bit confusing for them (and me!).

Aside from that, if the directory is empty there are no files to select - so
problem #2 they cannot select this directory.

Anyone got a solution please?
 
D

Dave Peterson

You could use application.getsaveasfilename. The file doesn't have to exist.

Option Explicit
Sub testme()

Dim myFolderName As Variant
myFolderName = Application.GetSaveAsFilename _
(InitialFileName:="FileNameWillBeIgnored")

If myFolderName = False Then
Exit Sub 'user hit cancel
Else
'your routine to strip out filename
End If
End Sub

Or you could use one of these routines...

Jim Rech has a BrowseForFolder routine at:
http://www.oaltd.co.uk/MVP/Default.htm
(look for BrowseForFolder)

John Walkenbach has one at:
http://j-walk.com/ss/excel/tips/tip29.htm


===
ps. If you and all your users are running xl2002+, take a look at VBA's help:
application.filedialog(msoFileDialogFolderPicker)
 
N

Nigel

Thanks Dave, good tip. I tried and used John Walkenbach version which is
ideal - thanks John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top