From: Kryten on
Hi,

Have run up against what, based on my web searches, is a very common
problem.

I want to incorporate a browseforfile dialog box in my script. I'm
using Windows 7.

Is there a simple way to do this, without resorting to references to
IE?

Thanks,
Stuart
From: mr_unreliable on
Kryten wrote:
> I want to incorporate a browseforfile dialog box in my script. I'm
> using Windows 7.
>

hi Kryten,

You may use the Browse_for_Folder dialog to Browse_for_FILE.

This may sound a little questionable, but it works, at least
for me. Try the following code (provided by "Joe Earnest").
Beware, some of the lines are "wrapped":

--- <code> ---
' 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...
'
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...
Set oFolder = oSHApp.BrowseForFolder(hDesktop, sTitle, vOptions,
vRootFolder)

' 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. NOTA BENE: 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

--- </code> ---

If this does work for you, you may question WHERE ARE THE FILES?

Try this, click on the little boxes-with-plus-signs. That should
open up the drives or folders and show the files.

Also, you might try reading further in msdn about what one can
do with the edit box. For example, I typed in "*.zip" and it
gave me back the "first" zip file it found. While I can''t
say for sure, this implies some "search" capabilities.
otoh, we already have "Find Files" for that.

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but, no guarantee
the answers will be applicable to the questions)
From: mayayana on
Doesn't the basic IE method work?

'---------------------
Function Browse()
On Error Resume Next
Dim Q2, sRet
Q2 = chr(34)
Browse = ""
Set IE = CreateObject("InternetExplorer.Application")
IE.visible = False
IE.Navigate("about:blank")
Do Until IE.ReadyState = 4
Loop

IE.Document.Write "<HTML><BODY><INPUT ID=" & Q2 & "Fil" & Q2 & "Type=" &
Q2 & "file" & Q2 & "></BODY></HTML>"
With IE.Document.all.Fil
.focus
.click
sRet = .value
End With
IE.Quit
Set IE = Nothing
Browse = sRet
End Function
'---------------------------

On most systems that shows a simple file browsing
window. I don't think I've tried it with IE8, though. If
it doesn't work then you probably need to use a 3rd-party
control. There are plenty of those, but they need to be
registered, so you wouldn't have a script that runs
anywhere without dependencies.

mr_unreliable's method may work, too. I seem
to remember a problem on at least some systems with
browsing to select a file. I thought that was disabled
with XP, but maybe I'm wrong. The other down side
of that method, of course, is that it's a bit hokey and
potentially confusing. People are not used to being
able to select files from a BrowseForFolder window.



From: mr_unreliable on
mayayana wrote:
> Doesn't the basic IE method work?
>

The way I read the original posting, the op didn't want
to "resort to references to IE".

cheers, jw
From: mr_unreliable on
Kryten wrote:
> I want to incorporate a browseforfile dialog box in my script.
>

hi Kryten,

If you happen to have ms's Common Dialogs control (comdlg32.ocx),
then you could use that:

--- <code> ---
Set oCD = WScript.CreateObject("MSComDlg.CommonDialog")

' set the dialog's titlebar caption...
oCD.DialogTitle = "Select the file you want to open"
oCD.MaxFileSize = 260
oCD.ShowOpen

MsgBox(oCD.FileName)
--- </code> ---

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but, no guarantee
the answers will be applicable to the questions)