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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to let user browse for a folder. 6

Status
Not open for further replies.

ziwacky

Programmer
Jun 27, 2000
43
US
I've been trying to search, with no luck so far, how to let user browse for a directory. That's it. Just the directory. I have routines that can search for the file (like the Microsoft Common Dialog, and the GetOpenFileName) But these take the user to select filename. All I need is let user browse for a folder or a directory.

I found in VB a SHBrowseForFolder function, but don't know how the heck use it. I know there gotta be a way.

Well, since this world is made up of ideas interchange, can you give me some light in here. Has anyone come up with anything in these?

Thanks
 
SHBrowseForFolder is an API function belonging to the Shell API. If you have MSDN, it's in the Platform SDK/User Interface Services/Windows Shell.

You can use the Shell API from Access 2000 (and probably Access 97, though I haven't tried that). First you must set a reference to Microsoft Shell Controls And Automation. (I'm using Window ME; it could be named slightly differently in Windows 98. I don't believe it existed in Windows 95.)

The following function uses SHBrowseForFolder (via the Shell32 library object model) and returns the folder path selected by the user. The user can also create a new folder using the dialog.

Code:
    Public Function GetFolder(Optional Instructions As String, _
                              Optional ShowEditBox As Boolean = False) As String
    ' Shows a shell folder browse window and allows user to select a folder.
    ' If Instructions is omitted or "", default is "Select a folder"
    ' If ShowEditBox is True, an edit box is displayed and user can type folder path
    ' Returns full folder path; if user cancels dialog, returns ""
    
        Dim shell As New Shell32.shell
        Dim folder As Shell32.folder
        Dim optns As Integer
        
        If Instructions = "" Then Instructions = "Select a folder"
        optns = IIf(ShowEditBox, 17, 1)
        Set folder = shell.BrowseForFolder(hWndAccessApp, Instructions, optns)
        If folder Is Nothing Then
            GetFolder = ""
        Else
            GetFolder = folder.Self.Path
        End If
        
        Set folder = Nothing
        Set shell = Nothing
    End Function
Rick Sprague
 
Rick,

I think that routine is great. There's only one thing. When I ran it it gives me error in the self and path properties for folder object. "folder.Self.Path". When I checked the property options it doesn't give me those as options. if i take the properties out, what I get is the name of the last folder and not the entire path.

I tried using the folder2 object, though ignoring what it really is, and folder2 has self and path as properties, but when trying to set the object(Set folder2 = shell.BrowseForFolder(hWndAccessApp, Instructions, optns)) got error msg on "type mismatch".
 
You're right! In my Object Browser, the Folder object doesn't have a Self property either. The odd thing is, the code works on my system anyway! (I copied it from my original message just to be sure I had the same code you have.) Maybe I have a different version of the Shell32 library (I'm using Windows ME).

Anyway, Folder2 should work for you. It wasn't necessary to change the folder variable name. All you really need is to change the Dim statement for the folder to:
Code:
    Dim folder As Shell32.Folder2

Hey, thanks for catching that for me. I'd added the function to my personal library. I might have used it in some production environment without ever knowing it was buggy. Then one day it'd fail and I'd be scratching my head wondering what happened! Rick Sprague
 
I'm afraid I'll have to dissapoint you, but using the folder2 also gives me error msg, because it doesn't accept the string that the browse form returns. It gives me "Type Mismatch" error.
 
Sorry you're having so much trouble. Is the "Type Mismatch" error on the Set statement, or the GetFolder = statement? Your last message said it "doesn't accept the string that the browse form returns", but the string is returned by the GetFolder = statement, so I'm not sure I know which line has the problem.

Check in your Object Browser. Shell.BrowseForFolder should return Folder. I believe Folder is assignment compatible with Folder2. Folder2.Self should return FolderItem, and FolderItem.Path should return String. The function is declared As String.

If all of these are correct in your copy of Shell32, I don't know what the problem could be. Try breaking the assignment statement into a Set statement like this:
Code:
    Dim fi As FolderItem
    
    Set fi = folder.Self
    GetFolder = fi.Path
Which statement then gets the Type Mismatch error? Rick Sprague
 
Bad news, and good news. Bad news is that I had to end up using another logic, which was much more longer and complicated than the one we were trying to use before. I don't even understand all of it. But, good news is that it worked like a charm, and since I copied it from the web, I didn't care what it had in it as long as it worked. So, Rick thanx a lot for your help. I just wish I had a source to go and look for help and examples on this things, because you logic was pretty simple to work on with the exemption of the error that was getting. I have two WIN32 API books in here, but in none of them could find anything related to the SHBrowseForFolder function. Probably I was looking for the wrong thing. Since I didn't know how to start debugging it, I had to go somehow else.
 
The code works, you have to use the item method of the folder object to get the path
"GetFolder = folder.Items.Item.Path".
 
I know this thread was started a year and a half ago, but I just had to say this. Great bit of coding!!!!

I have been trying to create a code snippet to do this for the last 7 hours with no success, and with a simple copy and paste job, this works PERFECT.

If you read this RickSPR, THANK YOU!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top