Browse for Folder
"D. Malaguti"
here a full example and it works with ALL os's
' Use BrowseForFolder to return file name...
' by: Joe Earnest, posted on vbScript ng, 09Jan03
'
' 11Jan03: added comments by jw (sorry Joe, couldn't resist)
' Note(jw): Haven't seen this particular edition of BFF before.
' For one thing, it comes with a textbox (i.e., an edit control)
' just under the "title" (i.e., a label). You may type any
' filespec or folder name you wish directly into the textbox.
' For another thing, the "TreeView" control is set up to show
' individual files as well as folders, and Joe's code shows
' how to retrieve the filename...
' --- end of discussion --------------------------
Option Explicit
'
Dim oSHApp : Set oSHApp= CreateObject("Shell.Application")
'
' Function Template:
' BrowseForFolder(Hwnd As Long, Title As String,
' Options As Long, [RootFolder]) As Folder
'
' Browsing for directory constants (from shlobj.h)...
Const BIF_RETURNONLYFSDIRS = &H0001 ' For finding a folder to start document searching
Const BIF_DONTGOBELOWDOMAIN = &H0002 ' For starting the Find Computer
Const BIF_STATUSTEXT = &H0004
Const BIF_RETURNFSANCESTORS = &H0008
Const BIF_EDITBOX = &H0010
Const BIF_VALIDATE = &H0020 ' insist on valid result (or CANCEL)
Const BIF_NEWDIALOGSTYLE = &H0040 ' (Version 5.0). use the new user interface.
' (you get a larger dialog box, with several new capabilities - see msdn)...
'
Const BIF_BROWSEFORCOMPUTER = &H1000 ' Browsing for Computers
Const BIF_BROWSEFORPRINTER = &H2000 ' Browsing for Printers
Const BIF_BROWSEINCLUDEFILES = &H4000 ' Browsing for Everything
'
' Shell Special Folder constants...
Const ssfDESKTOP = 0
Const ssfPROGRAMS = 2
Const ssfCONTROLS = 3
Const ssfPRINTERS = 4
Const ssfPERSONAL = 5
Const ssfFAVORITES = 6
Const ssfSTARTUP = 7
Const ssfRECENT = 8
Const ssfSENDTO = 9
Const ssfBITBUCKET = 10 ' a.k.a. recycle bin
Const ssfSTARTMENU = 11
Const ssfDESKTOPDIRECTORY = 16 ' (&H10)
Const ssfDRIVES = 17 ' (&H11) MyComputer Drives
Const ssfNETWORK = 18 ' (&H12)
Const ssfNETHOOD = 19 ' (&H13)
Const ssfFONTS = 20 ' (&H14)
Const ssfTEMPLATES = 21 ' (&H15)
Const ssfCOMMONSTARTMENU = 22 ' (&H16)
Const ssfCOMMONPROGRAMS = 23 ' (&H17)
Const ssfCOMMONSTARTUP = 24 ' (&H18)
Const ssfCOMMONDESKTOPDIR = 25 ' (&H19)
Const ssfAPPDATA = 26 ' (&H1A)
Const ssfPRINTHOOD = 27 ' (&H1B)
Const ssfLOCALAPPDATA = 28 ' (&H1C)
Const ssfALTSTARTUP = 29 ' (&H1D)
Const ssfCOMMONALTSTARTUP = 30 ' (&H1E)
Const ssfCOMMONFAVORITES = 31 ' (&H1F)
Const ssfINTERNETCACHE = 32 ' (&H20)
Const ssfCOOKIES = 33 ' (&H21)
Const ssfHISTORY = 34 ' (&H22)
Const ssfCOMMONAPPDATA = 35 ' (&H23)
Const ssfWINDOWS = 36 ' (&H24)
Const ssfSYSTEM = 37 ' (&H25)
Const ssfPROGRAMFILES = 38 ' (&H26)
Const ssfMYPICTURES = 39 ' (&H27)
Const ssfPROFILE = 40 ' (&H28)
Const ssfSYSTEMx86 = 41 ' (&H29)
Const ssfPROGRAMFILESx86 = 48 ' (&H30)
'
Dim oFolder ' as folder object
Const hDesktop = 0 ' use desktop as parent window...
Const sTitle = "Select a File or Folder... "
Dim vOptions : vOptions = BIF_BROWSEINCLUDEFILES _
Or BIF_NEWDIALOGSTYLE Or BIF_VALIDATE Or BIF_EDITBOX
Dim vRootFolder : vRootFolder = ssfDRIVES
'
Dim sResult ' as string
' --- end of constants and declarations ----------
' will return a (valid) file or folder...
'ORIGINAL
Set oFolder = oSHApp.BrowseForFolder(hDesktop, sTitle, vOptions, vRootFolder)
' Start nenu
Set oFolder = oSHApp.BrowseForFolder(hDesktop, sTitle, vOptions, ssfSTARTMENU)
' o.k., all the hard work is over, now interpret the results...
If (oFolder Is Nothing) then MsgBox("You clicked CANCEL") : WScript.Quit
' note: we insisted on a valid result, so if it's not nothing
' then the result must be a folder or file. BUT STILL, even if
' a file is returned the TypeName function will say folder object...
If (UCase(TypeName(oFolder)) = "FOLDER") Then ' verify folder
On Error Resume Next
sResult = oFolder.ParentFolder.ParseName(oFolder.Title).Path
If Err Then Err.Clear : sResult = oFolder.Title ' has no parent
On Error GoTo 0 ' turn off error processing
MsgBox("BFF returned: " & sResult)
Else ' something went wrong, it should have been "FOLDER"...
MsgBox("Unexpected result, BFF returned: " & oFolder.Title)
End If
--
Fosco
|