Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

Allow User to Choose Folder as Current Directory

Status
Not open for further replies.

predetc

Instructor
Sep 1, 2005
9
US
Hi,

I'd like to use VBA to allow a user to choose a folder to set as the new current directory. Is there a way to pop open a dialog box like the GetOpenFilename does, but choose a folder rather than a file?

Here's my start:

Dim Response As VbMsgBoxResult
Response = MsgBox("Your current directory is " & vbLf & _
CurDir & vbLf & _
"Would you like to change it?", vbYesNo, "Current Folder Directory")
If Response = vbYes Then
------------------------
End If

Thanks!
 
Try using the common dialog control with the filename property. Using that with this piece of code should give you what you need -

For i = Len(Me.CommonDialog.FileName) To 1 Step -1
x = Mid(Me.CommonDialog.FileName, i, 1)
If x = "\" Then tmp = i: Exit For
Next i
MsgBox Mid(Me.CommonDialog.FileName, 1, i)
 
Thanks, dinger2121. I actually wasn't quite sure about what the CommonDialog option was, but in researching I came across this code:

Application.FileDialog(msoFileDialogFolderPicker).Show
MyPath = CurDir


I think it may do what I need.

Thanks again for the reply!
 
I've already posted this function:
Code:
Function PickFolder(strStartDir As Variant) As String
Dim SA As Object, f As Object
Set SA = CreateObject("Shell.Application")
Set f = SA.BrowseForFolder(0, "Choose a folder", 16 + 32 + 64, strStartDir)
If (Not f Is Nothing) Then
  PickFolder = f.Items.Item.path
End If
Set f = Nothing
Set SA = Nothing
End Function

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top