
BrowseForFolder and default value
Hi Jon,
Just for info. The reason you're getting sporatic
results is that the BrowseForFolder &H4000 file
option will generate an incurable error on WinXP.
BrowseForFolder has somewhat different bit codes
on WinXP, even when used for folders, and a different
"trick" to allow it to return root folders without error.
(See the previously posted code.)
For a file dialog across all OS's, there's the Tom
Lavedas-Walter Zachary ChooseFile HTML trickery
(which I've modified with a 0-size window per Alex
Angelopoulos for better focus on Win2k & WinXP).
For WinXP only, there's a new User ComDlg object
that allows file filtering.
-----
' ShowChooseFile Demo
Option Explicit
Dim sDflt
sDflt= "C:\" ' insert your default file or folder
WScript.Echo ShowChooseFile(sDflt)
WScript.Quit
Function ShowChooseFile (ByVal vsDfltSln)
' displays the HTML file Choose File dialog with a default using a
' 0-size InternetExplorer.Application window
' InternetExplorer.Application access is local, the window is closed
' and access is disconnected on exit
' InternetExplorer.Application, Shell and FileSystemObject access
' are required
' returns
' file
' a file other than the default input was selected,
' and Open was pressed
' null string
' a file was not selected, and/or Cancel, [x] or Esc was pressed, or
' the default input was selected, or there was an object access error
' vsDfltSln= the default file or folder path string
' defaults to null
Dim vcTime
Dim voFileSys, voIEApp, voShell
Dim vqAIX
Dim vsDflt, vsSln, vsTitle
ShowChooseFile= ""
On Error Resume Next
Set voIEApp= CreateObject("InternetExplorer.Application")
If Err Then Err.Clear: Exit Function
Set voShell= CreateObject("WScript.Shell")
If Err Then Err.Clear: Exit Function
Set voFileSys= CreateObject ("Scripting.FileSystemObject")
If Err Then Err.Clear: Exit Function
On Error Goto 0
vsTitle= voFileSys.GetBaseName(voFileSys.GetTempName)
vsDflt= ""
If (VarType(vsDfltSln)=vbString) Then vsDflt= Trim(vsDfltSln)
On Error Resume Next
voIEApp.Navigate "about:blank"
If Err Then Err.Clear: Exit Function
On Error GoTo 0
Do: WScript.Sleep 50: Loop Until (voIEApp.ReadyState=4)
With voIEApp
.AddressBar = False
.MenuBar = False
.ToolBar = False
.StatusBar = False
.Height = 0
.Width = 0
.Resizable = False
With .Document
With .ParentWindow
.Moveto .Screen.Width \2, .Screen.Height \2
End With
.Write ("<input type=""file"" id=""choosefile"">")
.Title = vsTitle
.All.choosefile.Focus
End With
.Visible = True
End With
With voShell
For vcTime= 1 To 40
WScript.Sleep 50
If .AppActivate(vsTitle) Then
WScript.Sleep 50
.SendKeys vsDflt
Exit For
End If
Next
End With
voIEApp.Document.All.choosefile.Click
vsSln= voIEApp.Document.All.choosefile.Value
If LCase(vsSln)= LCase(vsDflt) Then vsSln= ""
With voShell
For vcTime= 1 To 40
WScript.Sleep 50
If .AppActivate(vsTitle) Then
WScript.Sleep 50
.SendKeys "%{F4}"
Exit For
End If
Next
End With
ShowChooseFile= vsSln
End Function
-----
-----
' ShowUserComDlg Demo
' for WinXP only
Option Explicit
Dim sDflt
sDflt= "C:\*.*" ' insert your default file or path+mask
sFilter= "" ' insert your filter list
WScript.Echo ShowUserComDlg(sFilter, sDflt)
WScript.Quit
Function ShowUserComDlg (ByVal vsFilterList, ByVal vsDfltSln)
' displays the UserAccounts.CommonDialog Open dialog
' for WinXP only
' returns
' file
' a file was selected or specified and Open was pressed
' null string
' a file was not selected or specified and/or
' Cancel, [x] or Esc was pressed, or
' UserAccounts.CommonDialog access error
' vsFilterList=
' the standard Common Dialog paired file filter list string
' with pipe ( | ) delimiters, of descriptions and corresponding file
' extension lists with semi-colon ( ; ) delimiters
' "display text | *.ext [ ; ... ] [ | ... | ... ]"
' typically
' "... FILES (*.ext [ , ... ] ) | *.ext [ ; ... ] [ | ... | ... ]"
' defaults to "All Files (*.*)|*.*"
' opens to the the first-listed filter and resulting files
' vsDfltSln= the default file path string
' opens the dialog to the file's folder, the file name is required,
' a mask ( "*.*" ) may be used in lieu of a file name
' defaults to null
Dim voComDlg, vsDflt, vsFilter, vsSln
ShowUserComDlg= ""
On Error Resume Next
Set voComDlg= CreateObject("UserAccounts.CommonDialog")
If Err Then Err.Clear: Exit Function
On Error GoTo 0
vsDflt= ""
If (VarType(vsDfltSln)=vbString) Then
vsDflt= Trim(vsDfltSln)
End If
vsFilter= "All Files (*.*)|*.*"
If (VarType(vsFilterList)=vbString) Then
If InStr(vsFilterList, "|") Then vsFilter= vsFilterList
End If
voComDlg.Filter= vsFilter
voComDlg.FilterIndex= 1
If NOT (vsDflt="") Then voComDlg.FileName= vsDflt
vsSln= ""
If voComDlg.ShowOpen Then vsSln= voComDlg.FileName
ShowUserComDlg= vsSln
End Function
----
Joe Earnest
Quote:
> This may help, but i had trouble getting it going - seems to work on some
> pc's and not others...
> Function BrowseForFile(sPMT,sDIR)
> 'sPMT - Prompt Message Test
> 'sDIR - Start Directory
> Dim objSHL
> Dim objB4F
> Dim s
> BrowseForFile = ""
> Set objSHL = CreateObject("Shell.Application")
> On Error Resume Next
> Set objB4F = objSHL.BrowseForFolder(&H0,sPMT,&H4000,sDIR)Set
> S=objB4F.ParentFolder.items.item
> BrowseForFile=s.path&"\"&objB4F
> If Err.Number <> 0 Then BrowseForFolder = ""
> '*
> Set objB4F = Nothing
> Set objSHL = Nothing
> End Function
> Jon
Quote:
> > Hi all.
> > I'm using BrowseForFolder method as folder picker but coudn't find how
to
> > pass default path to "edit" box.
> > Is there any technics to setup default against this dialog object?
> > Thank you very much.