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

Can't get BrowseForFolder to display file

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
I got the following code off the net. It pops up a browse dialog window from which I should be able to choose a file or folder. I can choose the file I want, but the code fails to tell me which file I've chosen.

I pulled other examples off the net with the same results. That is, the code I've found always returns the folder selected. But I can't find an example that returns/displays the file I've chosen.

Any ideas?

Code:
Option Explicit

MsgBox BrowseForFolder("File","Browse a File")
MsgBox BrowseForFolder("Folder","Browse a Folder")

Function BrowseForFolder(sBFF,sPMT)
    BrowseForFolder = ""
    If sBFF <> "Folder" And sBFF <> "File" Then Exit Function
   '*
    Dim objSHL
    Set objSHL = CreateObject("Shell.Application")
    Dim objB4F
   '*
    On Error Resume Next
    If sBFF = "Folder" Then
        Set objB4F = objSHL.BrowseForFolder(&H0,sPMT,&H0031,&H0011)
    Else
        objB4F = objSHL.BrowseForFolder(&H0,sPMT,&H4031,&H0011)

    End If
    BrowseForFolder = objB4F.ParentFolder.ParseName(objB4F.Title).Path
    If Err.Number <> 0 Then BrowseForFolder = ""
   '*
    Set objB4F = Nothing
    Set objSHL = Nothing
End Function
 
Since I couldn't get this to work, found a different way of doing it at thread329-696121.
 
Hello FancyPrairie,

It is called BrowseForFolder not without a reason. I'm not saying it's something enimatic, it is.

[1] You can always use .self.path to retrieve the path of the folder/file selected.
Code:
    BrowseForFolder = objB4.self.path
Not that there is anything wrong with what you used in the script, the .self.path looks simpler, more intuitive to remember.
[2] You differentiate file & folder. For file, you do not put a set keyword. This is definitely a mistake. (It is probably due to the misleading behavior as discussed in [3])
[3] For file to be retrieved correctly, apart from the BIF_BROWSERINCLUDEFILE=&H4000 is needed in the iOptions parameter, it must satisfy two conditions.
[3.1] It is not directly under a root, such as d:\.
[3.2] It must not be under the ending leaf of the folder-tree. For instance, d:\abc\xyz is a directory, under which there is no subdirectory. Then those files under it will be not displayed, and hence, retrieved with a reference.
[4] It is _slow_ and possibly with security problems to uncover. Hence, it is not a highly recommended thing to do with script. (I will not say the same for vb equivalent.)

Apart from these caveats, The following the revision you can verify these remarks. (I have not been tempted to make it robust, though.)
Code:
Option Explicit

MsgBox BrowseForFolder("File","Browse a File")
MsgBox BrowseForFolder("Folder","Browse a Folder")

Function BrowseForFolder(sBFF,sPMT)
    BrowseForFolder = ""
    If sBFF <> "Folder" And sBFF <> "File" Then Exit Function
   '*
    Dim objSHL
    Set objSHL = CreateObject("Shell.Application")
    Dim objB4F
   '*
    On Error Resume Next
    If sBFF = "Folder" Then
        Set objB4F = objSHL.BrowseForFolder(&H0,sPMT,&H0031,&H0011)
    Else
        Set objB4F = objSHL.BrowseForFolder(&H0,sPMT,&H4031,&H0011)
    End If
    'BrowseForFolder = objB4F.ParentFolder.ParseName(objB4F.Title).Path
    BrowseForFolder = objB4F.Self.Path
    If Err.Number <> 0 Then BrowseForFolder = ""
   '*
    Set objB4F = Nothing
    Set objSHL = Nothing
End Function
regards - tsuji
 
tsuji, appreaciate your time and concerns. I tryed using your code and still no luck. objB4F always returns "Empty" to me no matter where the file is located in the path.

But based on your concerns, it appears that the browse feature of the input statement is a better way to go anyway (at least for files).
 
FancyPrairie,

It is not that cool after all for scripting with that functionality. Just a note. This functionality is dependent on the api coded in shell32.dll. It does evolve often without much fanfare. Hence, it is dependent on the os and ie.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top