Select folder vba

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
934
I found this code that will allow the user to browse the computer for a file. Its close to what I'm looking for, but not sure how to edit it to fit what I'm trying to accomplish.

I created a userform. On it, it has a label with the text "Example1". I have Browser button and a textbox. I would like the user to be able to select the folder, but not change the name of the downloaded file "Example1". Is there a way to allow the user to select the location on the computer, insert the Label's name "Example1" in the save dialogue box and not allow the user to change the name?

[VBA]txtFile = Application.GetSaveAsFilename(InitialFileName:=vbNullString, _
FileFilter:="Text Files (*.txt;*.csv), *.txt;*.csv", Title:="Select File Name")[/VBA]
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
934
ok...

here's what i found it does the trick, how to i show the (c:\Document) in the textbox.

Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long

Private Const BIF_RETURNONLYFSDIRS = &H1
Public Function BrowseFolder(szDialogTitle As String) As String
Dim X As Long, bi As BROWSEINFO, dwIList As Long
Dim szPath As String, wPos As Integer

With bi
.hOwner = hWndAccessApp
.lpszTitle = szDialogTitle
.ulFlags = BIF_RETURNONLYFSDIRS
End With

dwIList = SHBrowseForFolder(bi)
szPath = Space$(512)
X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)

If X Then
wPos = InStr(szPath, Chr(0))
BrowseFolder = Left$(szPath, wPos - 1)
Else
BrowseFolder = vbNullString
End If
End Function

Private Sub cmdBrowse_Click()

Dim strFolderName As String

strFolderName = BrowseFolder("Choose Folder For Import")

If Len(strFolderName) > 0 Then
' Do something with the selected folder
Else
'No folder chosen, or user canceled
End If

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,109,486
Messages
5,529,152
Members
409,851
Latest member
Ingar
Top