Using the following code, the selected Folder name does not show in the
'Folder name:' comboBox one prior to 'OK'. Nothing appears except a choice from the dropdown list. Tried in Excel 2003 and 2007.
The function returns the right foldername though.
This is the same on 2 computers but a third (so I'm told) does show the name.
Code:
Function BrowseFolder()
'Open the file dialog
Dim RetVal As String
Set BrowseFolder = Application.FileDialog(msoFileDialogFolderPicker)
With BrowseFolder
.Show
If BrowseFolder.SelectedItems.Count > 0 Then
RetVal = .SelectedItems(1)
End If
End With
BrowseFolder = RetVal
End Function
What should it do and can I make it show the foldername anyhow?
many dialogs can retain previous values if used in the same session, this maybe why it can work on 1 machine
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Re: Question re FileDialog(msoFileDialogFolderPicker)
well it is a file picker, not a folder browser
search on shbrowseforfolder
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Re: Question re FileDialog(msoFileDialogFolderPicker)
Ok, I get it..
In that case, why don't you use this instead...
Code:
Sub Sample()
FolderBrowser "C:"
End Sub
Function FolderBrowser(Optional DefaultLocation As Variant) As Variant
Dim ShApp As Object
Set ShApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, DefaultLocation)
On Error Resume Next
FolderBrowser = ShApp.self.Path
On Error GoTo 0
Set ShApp = Nothing
Select Case Mid(FolderBrowser, 2, 1)
Case Is = ":"
If Left(FolderBrowser, 1) = ":" Then GoTo SubExit
Case Is = "\"
If Not Left(FolderBrowser, 1) = "\" Then GoTo SubExit
Case Else
GoTo SubExit
End Select
Exit Function
SubExit:
FolderBrowser = False
End Function
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Re: Question re FileDialog(msoFileDialogFolderPicker)
Thanks Koolsid. That was worth a go, but doesn't let you go back a level if you re-use the last Path.
shbrowseforfolder can't remember last path and always starts at the root.
msoFileDialogFolderPicker is OK in Win7 but fails as described in XP.
If no more suggestions I'll detect the OS and use FilePicker or shbrowseforfolder.
Malcolm Fraser was right - life wasn't meant to be easy !
Re: Question re FileDialog(msoFileDialogFolderPicker)
in kools example
if you pass a value for defaultlocation the browse dialog should start at that level, you can store this from the last call
this is just using a shell object for the same as shbrowseforfolder
Last edited by westconn1; Dec 7th, 2010 at 09:23 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Re: Question re FileDialog(msoFileDialogFolderPicker)
Yes, I understand... but the reason for looking at alternatives to shBrowseFolder was to store and return to the last path used.... avoiding having to navigate all the way back through the same folders again. But you need to be able to go both ways from there. A default location is not really the solution as it would have to be C:\ which takes us back where we were.
Just a pity FolderPicker works in Windows 7 and not XP, but finding why is far beyond me !
Thank you both for the help and suggestions, though.
Regards, ABB