Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting files with BrowseForFolder method

3,496 views
Skip to first unread message

Dave "Crash" Dummy

unread,
May 22, 2010, 7:51:28 AM5/22/10
to
I have a script function that I used successfully in Windows 2000, but
does not work fully in Windows 7 (x64). The test script below works fine
at returning folder paths and file paths for files with .htm/.html
extensions. It fails for files with any other extension (.txt, .hta,
.vbs tried). As configured, the function returns an error number of 424
when it malfunctions. If "On Error Resume Next" is removed, the
malfunction generates a popup specifiying an unknown error number
80004000 for the line containing the BrowseForFolder method.

I'd sure like to use this or a similar script to select ALL file types.

'================= getFile.vbs ==========================
msgbox getFile()

function getFile()
Set objShell = CreateObject("Shell.Application")
on error resume next
Set objFolder = objShell.BrowseForFolder(0, "Select an author:",
&H4000& , "e:\books")
Set objFolderItem = objFolder.Self

if err.number = 0 then
getFile=objFolderItem.path
else
getFile=err.number
err.clear
end if
end function
'=====================================================

--
Crash

"The real question is not whether machines think but whether men do."
~ B. F. Skinner ~

Mayayana

unread,
May 22, 2010, 10:10:14 AM5/22/10
to
If you search on: BrowseForFolder return files XP
you'll get all sorts of returns like this one:

http://www.webmasterkb.com/Uwe/Forum.aspx/vbscript/4253/BrowseForFolder

The long and the short of it is don't
use BrowseForFolder to return files. It's
not designed for that. It's also part of
the Shell ops, which change on each
Windows version.

As far as I know the IE method works
fine on all systems. It has limited options,
but BrowseForFolder doesn't have many
options either.

MsgBox Browse()

Function Browse()
Dim IE, Q2
On Error Resume Next
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

If for some reason that method won't work
on Win7-64 then you'll probably just have to
use a 3rd-party option.

Dave "Crash" Dummy

unread,
May 22, 2010, 10:40:53 AM5/22/10
to

Thanks! That works. I can adapt ot for my needs.
--
Crash

"In politics, stupidity is not a handicap."
~ Napoleon Bonaparte ~

Dave "Crash" Dummy

unread,
May 22, 2010, 10:59:41 AM5/22/10
to
Is there some way to have the script open to a specified root
(D:\folder) instead of the desktop?
--
Crash

English is not my native tongue; I'm an American.

Mayayana

unread,
May 22, 2010, 11:36:07 AM5/22/10
to
| Is there some way to have the script open to a specified root
| (D:\folder) instead of the desktop?
| --
I'm not certain whether this is consistent
across systems, but I just tried a test and
found the following:

Setting WScript.Shell CurrentDirectory had
no effect.

Looking for an MRU list in the Registry turned up
nothing, but I suspect IE keeps an MRU list there.

In my use of the script it was opening to F drive,
a data partition. I then opened IE (IE6/XP) and
manually browsed to an HTML file on K drive, in
a subfolder. When I ran the script again the File
Open started in that K-drive subfolder.

So *it looks* like you can select the opening
location by writing a file to the selected folder
and opening that in IE before running the Browse
function. Of course that won't stop anyone from
going up the tree, like BrowseForFolder can do. It
just lets you pick the "start in" folder.


Dave "Crash" Dummy

unread,
May 22, 2010, 12:24:41 PM5/22/10
to
Mayayana wrote:

> I'm not certain whether this
> is consistent across systems, but I just tried a test and found the
> following:
>
> Setting WScript.Shell CurrentDirectory had no effect.
>
> Looking for an MRU list in the Registry turned up nothing, but I
> suspect IE keeps an MRU list there.
>
> In my use of the script it was opening to F drive, a data partition.
> I then opened IE (IE6/XP) and manually browsed to an HTML file on K
> drive, in a subfolder. When I ran the script again the File Open
> started in that K-drive subfolder.
>
> So *it looks* like you can select the opening location by writing a
> file to the selected folder and opening that in IE before running the
> Browse function. Of course that won't stop anyone from going up the
> tree, like BrowseForFolder can do. It just lets you pick the "start
> in" folder.

Thanks. Selecting the "Start in" folder is what I want to do. It doesn't
look like there is any easy way to do it. I'll just have to play around.
--
Crash

"It is not necessary to change. Survival is not mandatory."
~ W. Edwards Deming ~

Tom Lavedas

unread,
May 24, 2010, 9:20:25 AM5/24/10
to
On May 22, 12:24 pm, "Dave \"Crash\" Dummy" <inva...@invalid.invalid>
wrote:

As you have found, the FILE type of the HTML INPUT element does not
permit a default (starting) location. This is an appropriate security
feature for web applications. The only workaround I have found is to
use Sendkeys to pass the starting location into the procedure,
something like in the example below ...

' For example, ...
MyDocs = CreateObject("WScript.Shell")_
.SpecialFolders("MyDocuments") & "\*"
Wscript.echo ChooseFile(MyDocs)

' Adapted by Tom Lavedas from an example by Walter Zackery
' Requires WSH v2+ (aka 5.1+) to set StartIn location

Function ChooseFile(StartIn)
set IEApp = CreateObject("InternetExplorer.Application")
' Required for absolute positioning
IEApp.fullscreen = True
' Hide IE window
IEApp.height = 0 : IEApp.width = 0
' Position Upper Left Corner (ULC)
IEApp.Left = 200 : IEApp.Top = 200
IEApp.Navigate("about:blank")
Do Until IEApp.ReadyState = 4
if Wscript.Version > 5 Then WScript.Sleep 100
Loop
IEApp.document.open
IEApp.document.write "<input id=file type=file"_
& " style='position:absolute; left:0; top:0'>"
IEApp.document.close
Do Until IEApp.document.Readystate = "complete"
if Wscript.Version > 5 Then Wscript.Sleep 100
Loop
IEApp.Visible = true
set oDoc = IEApp.document
if Wscript.Version > 5 Then
Do Until CreateObject("Wscript.Shell").AppActivate("about")
WSH.Sleep 50
Loop
End if
oDoc.all.file.focus
if Wscript.Version > 5 Then
CreateObject("Wscript.Shell").Sendkeys StartIn
WSH.Sleep 100
End if
oDoc.all.file.click ' opens dialog window
IEApp.Visible = false
' The file dialog is modal, so no wait loop is required.
ChooseFile = oDoc.all.file.value
set oDoc = Nothing
set IEApp = Nothing
if ChooseFile = StartIn Then _
ChooseFile = "" 'that is, Canceled
End Function ' ChooseFile
_____________________
Tom Lavedas

Dave "Crash" Dummy

unread,
May 24, 2010, 12:48:37 PM5/24/10
to
Tom Lavedas wrote:
> On May 22, 12:24 pm, "Dave \"Crash\" Dummy" <inva...@invalid.invalid>
> wrote:
>> Mayayana wrote:
>>> I'm not certain whether this
>>> is consistent across systems, but I just tried a test and found the
>>> following:
>>> Setting WScript.Shell CurrentDirectory had no effect.
>>> Looking for an MRU list in the Registry turned up nothing, but I
>>> suspect IE keeps an MRU list there.
>>> In my use of the script it was opening to F drive, a data partition.
>>> I then opened IE (IE6/XP) and manually browsed to an HTML file on K
>>> drive, in a subfolder. When I ran the script again the File Open
>>> started in that K-drive subfolder.
>>> So *it looks* like you can select the opening location by writing a
>>> file to the selected folder and opening that in IE before running the
>>> Browse function. Of course that won't stop anyone from going up the
>>> tree, like BrowseForFolder can do. It just lets you pick the "start
>>> in" folder.
>> Thanks. Selecting the "Start in" folder is what I want to do. It doesn't
>> look like there is any easy way to do it. I'll just have to play around.
>
> As you have found, the FILE type of the HTML INPUT element does not
> permit a default (starting) location. This is an appropriate security
> feature for web applications. The only workaround I have found is to
> use Sendkeys to pass the starting location into the procedure,
> something like in the example below ...

<snipped>

Sheesh! Life used to be so much simpler! Thanks, Tom. I'll add it to my
collection.
--
Crash

Committed to the search for intraterrestrial intelligence.

Mayayana

unread,
May 24, 2010, 11:27:12 PM5/24/10
to
| Sheesh! Life used to be so much simpler!

I just came across an interesting thing. mshtmled.dll
seems to be on all systems, and has the
HTMLDlgHelper.HTMLDlgHelper object. It's not safe
for scripting, and is a control that can't be created in
a script. But in an HTA it provides the full functionality
of a system API FileOpen dialog. It also has FileSave
and a functional but buggy ChooseColor function.

I came across a very roundabout method to use
the object from straight script, but it appears to
be taking advantage of a security bug that allows
an HTML to get HTA security status. I'm looking at
finding a more workable method to make it work
from a script, but no luck so far.

Below is a sample that works in an HTA. In HTML
it's blocked by security restrictions. I've only tried
this on XP, but it seems to be on all Windows
versions:

<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE="VBScript">
Dim sPath
Sub But_onclick()
sPath = OpenFileDialog("E:\js comps\*.*", "GIF JPG BMP, gif jpg bmp, Text
Files, txt, HTML Files, htm html, All Files, *", "Choose File")
MsgBox sPath
End Sub

Function OpenFileDialog(StartPath, Filter, Caption)
Dim A1, sFilter, i2, s1
A1 = Split(Filter, ",")
i2 = 0
Do While i2 < UBound(A1)
sFilter = sFilter & Trim(A1(i2))
s1 = Trim(A1(i2 + 1))
s1 = "*." & s1
s1 = Replace(s1, " ", ";*.")
sFilter = sFilter & "(" & s1 & ")|" & s1 & "|"
i2 = i2 + 2
Loop
OpenFileDialog = Dlg.openfiledlg(CStr(StartPath), , CStr(sFilter),
CStr(Caption))
End Function
</SCRIPT>
</HEAD>
<BODY>
<object id="Dlg" classid="CLSID:3050F4E1-98B5-11CF-BB82-00AA00BDCE0B"
width=100 height=100></object>
<INPUT ID="But" TYPE="button" VALUE="Browse"></INPUT>
</BODY></HTML>

-----------------------
Two notes: 1) The CStr seems to be necessary for it to work.
2) The filter is a specifically constructed string that allows
any number of custom filter options in the dropdown. It's
clear how that works when you run the sample. But the filter
string must be just so. For that reason I wrote the function
to accept a simple, comma-delimited list. It's self-explanatory.

So this function allows for specifiying a caption, setting the
starting folder/file, and setting any number of specific filters.


Dave "Crash" Dummy

unread,
May 25, 2010, 6:58:01 AM5/25/10
to

Thank you. As a matter of fact, my need for a "Browse for File" function
is often from a HTA base. A quick cut and paste (and unwrapping the
wrapped lines) of your script works on my machine. It will take a
little longer for me to understand ll the variables. :-)
--
Crash

"Something there is that doesn't love a wall, that wants it down."
~ Robert Frost ~

Dave "Crash" Dummy

unread,
May 25, 2010, 6:59:43 AM5/25/10
to

Thank you. As a matter of fact, my need for a "Browse for File" function

Dave "Crash" Dummy

unread,
May 25, 2010, 8:55:34 AM5/25/10
to
Did some fooling around with your script and discovered a few things.
First, the dlg object can be zero dimension (width=0 height=0), which
eliminates any layout problems. Also, the filter is optional. The
openfiledlg method works fine with a zero length filter string:

getFilePath=Dlg.openfiledlg(CStr(StartPath), , , CStr(Caption))

Restrictions to displayed file type, if desired, can be set with the
StartPath string, for example "e:\books\*.txt". Unlike the optional
filter, this is limited to one file type at a time, but one thing I
checked is that "d:\folder\*.htm" will also include *.html.

Mayayana

unread,
May 25, 2010, 9:13:26 AM5/25/10
to

| Thank you. As a matter of fact, my need for a "Browse for File" function
| is often from a HTA base. A quick cut and paste (and unwrapping the
| wrapped lines) of your script works on my machine. It will take a
| little longer for me to understand ll the variables. :-)
| --

I'm hoping others will see this and test it on
different Windows versions. It appears that
mshtmled.dll goes back to Win9x, as a system file
used by IE.

The functions as shown in a typelib:

Method openfiledlg([initFile as Variant][, initDir as Variant][, filter as
Variant][, title as Variant]) as String

Method savefiledlg([initFile as Variant][, initDir as Variant][, filter as
Variant][, title as Variant]) as String

Method choosecolordlg([initColor as Variant]) as Long

There's also a Document object, but I assume
one must be using the control in software with
a browser window to use that.

I've tried different ways of passing an empty
1st parameter and just passing the folder path
(2nd parameter) but I haven't got it to work. So
I wrote the function to skip the second parameter.
You can specify a file like Windows does in the
"Setup requires a missing file..." dialogue. I wrote
it with wildcard like C:\Windows\*.*, but you can
also do something like C:\Windows\notepad.exe.
That's a nice touch. However, if the default filter
does not include EXE files then notepad.exe won't
be visible, even though it's selected.

The last paramter is the dialogue caption.

The 3rd parameter is tricky. mshtmled.dll, it turns
out, works the same way as comdlg32.ocx that comes
with VB. The ocx wraps the Win32 API functions in
comdlg32.dll, but requires a license.
mshtmled.dll does the same,
wrapping GetOpenFileName, GetSaveFileName, etc.
The strange design of the Filter string is connected
with that. The API function requires a null character
to separate entries. The wrappers use a pipe character
as a substitute and then convert it before sending it
on to comdlg32.dll. Comdlg32.dll requires exact syntax
or the call won't work. The way it functions is that
you can create any number of entries in the "File of type"
dropdown. Each will filter the folder view to show only
those files. Traditionally an All Files entry is used at
the end. The default filter is the first. (In this case
GIF JPG BMP.) I wrote the sample filter to show the
different options. You can have any number of filters.
All must have the same syntax but there can be multiple
file extensions in a filter. The file type name is whatever
you want it to be. So...

Text Files(*.txt)|*.txt|Dave Crash Dummy Image
Files(*.bmp;*.png)|*.bmp*.png|

That creates two items in the dropdown. One can only
choose a TXT, BMP, or PNG file. If there are multiple
file extensions in a filter they're separated by a semi-colon.
Otherwise it's standard for all filters:

Name, then "*.ext" in parentheses, then pipe, then "*.ext"
not in parentheses, then an ending pipe.

You can have as any filters as you like, just as PhotoShop
or OpenOffice, say, will allow you to save or open numerous
types of files.

The filter syntax design is so clunky that it's easy to mess
up. That's why I designed my version with the filter it has.
That's the simplest version I could think of. It's just a
comma-delimited string with name, ext, name, ext, name, ext,
etc. Multiple extensions are sent with a space:
Image Files, bmp jpg gif

That method allows for very complex filters, while
the function doesn't need to do much work in order to
convert the incoming string into a usable filter.

The CStr bit is something I don't understand. According
to the typelib the parameters are all variants. But they
weren't working if I sent a variable rather than a literal.
So I tried CStr and that worked.

If you look up the GetOpenFileName API you'll see
that the mshtmled.dll functions provide nearly all
of the same functionality. The notable thing missing
is that openfiledlg does not have a Flags parameter.
Among other things that means that you can't specify
the dialogue to be multi-select.

I haven't tried the Save dialogue. I tried the color
dialogue but so far I haven't got the "start-with" color
to work. Also, the window opens to a limited selection.
One has to click "Define Custom Colors" to view the
full color selector. And the color clicked in the selector
does not register until one first clicks either the right-hand
"shade bar" or one of the left-hand color boxes. Other
than those bugs it works. :)
I wrote a little function to return RGB hex from the
long integer that choosecolordlg returns:

Function ChooseColor(InitColor)
Dim LCol, sCol
LCol = Dlg.choosecolordlg(InitColor)
sCol = Right(("0000000" & Hex(LCol)), 6)
ChooseColor = Right(sCol, 2) & Mid(sCol, 3, 2) & Left(sCol, 2)
End Function


Mayayana

unread,
May 25, 2010, 10:27:51 AM5/25/10
to
| First, the dlg object can be zero dimension (width=0 height=0), which
| eliminates any layout problems.

Woops. Yes, it doesn't seem to need any
width/height setting at all. That code was
left over from my various experiments.


Dave "Crash" Dummy

unread,
May 25, 2010, 11:35:01 AM5/25/10
to

Not so. If you leave the width/height settings out, the box will default
to 240x240 (may be different with different screen resolutions).

Mayayana

unread,
May 25, 2010, 12:52:27 PM5/25/10
to
| Not so. If you leave the width/height settings out, the box will default
| to 240x240 (may be different with different screen resolutions).

Interesting. That seems to be right. If I leave it out
there's no box, so I assumed the width/height were not
necessary. But I see now that it affects the position of
the Browse button. So I don't see the control but it is
taking up space.

I'm hoping that others will chime in here to report
whether this dialogue on other systems and with other
IE versions. This object won't be of much use if it isn't
known to work on all systems.

My PC: XP SP3, IE6.


Dave "Crash" Dummy

unread,
May 25, 2010, 3:16:38 PM5/25/10
to
Mayayana wrote:
> | Not so. If you leave the width/height settings out, the box will default
> | to 240x240 (may be different with different screen resolutions).
>
> Interesting. That seems to be right. If I leave it out
> there's no box, so I assumed the width/height were not
> necessary. But I see now that it affects the position of
> the Browse button. So I don't see the control but it is
> taking up space.

Insert this in the object tag: "Style=background:red"
That will make it visible.

> I'm hoping that others will chime in here to report
> whether this dialogue on other systems and with other
> IE versions. This object won't be of much use if it isn't
> known to work on all systems.
>
> My PC: XP SP3, IE6.

I mentioned it at the start of this thread, but to repeat,

My PC : 64 bit Windows 7 Ultimate, IE8
--
Crash

"When you want to fool the world, tell the truth."
~ Otto von Bismarck ~

Tom Lavedas

unread,
May 26, 2010, 11:11:53 AM5/26/10
to
On May 25, 3:16 pm, "Dave \"Crash\" Dummy" <inva...@invalid.invalid>
wrote:

For anyone who has followed this thread, as was stated, the
OpenFileDialog object cannot be accessed in anything but an HTA, not
even a scripted IE.Application script. That still leaves the object
'unsafe for scripting". So to work around that restriction and make
the dialog available for standalone VBS, I built this wrapper script
example ...

'-------- main --------
set FSO = CreateObject("Scripting.FileSystemObject")

' Generate an HTA wrapper of dialog object
HTA = MakeHTA(FSO) 'as Array: 0 is window, 1 is temp path

' Pass the HTA's window to access the Dlg object
sPath = OpenFileDialog(HTA(0),"h:\*", _
"All Files, *, Images, gif jpg bmp, Text Files, txt, " _
& "HTML Files, htm html", "Choose File")

HTA(0).Close ' Close the wrapper HTA
FSO.DeleteFile HTA(1) ' remove temp HTA file

' For example
wsh.echo sPath
'-------- end main --------

Function OpenFileDialog(Window, StartPath, Filter, Caption)


Dim A1, sFilter, i2, s1

A1 = Split(Filter, ",")

for i2 = 0 to UBound(A1) step 2


sFilter = sFilter & Trim(A1(i2))
s1 = Trim(A1(i2 + 1))

s1 = Replace("*." & s1, " ", ";*.")


sFilter = sFilter & "(" & s1 & ")|" & s1 & "|"

Next

' Access the wrapper to gain access to the Dialog
OpenFileDialog = Window.OpenFileDialog(StartPath, sFilter, Caption)

End Function ' OpenFileDialog

Function MakeHTA(ofs) ' as Array(1)
Dim ID, stemp, oWS, sTitle, WdW, oWdw, bFound

' Generate a random ID
Randomize
ID = CLng((10000000 - 100000 + 1) * Rnd + 100000)

' Generate a temporary file name for the HTA
set oWS = CreateObject("Wscript.Shell")
stemp = oWS.Environment("PROCESS")("TEMP") & "\" _
& ofs.GetTempName & ".hta"

' Create an auxilary HTA to host the OpenFileDialog object
sTitle = "::" ' Character(s) to make it small and unique
With ofs.OpenTextFile(sTemp, 2, True)
.writeline "<HTML><HEAD><TITLE>" & sTitle & "</TITLE></HEAD>"
.writeline "<script language=vbs>resizeto 0,0:moveto 400,300</
script>"
.writeline "<HTA:Application maximizeButton=no minimizeButton=no "
.writeline "sysMenu=no showInTaskBar=no/><SCRIPT language=vbs>"

.writeline "Sub Window_onload"
.writeline "' Create unique ID for the WebBrowser object"
.writeline " WebBrowser.PutProperty ""ID""," & ID
.writeline "end sub"

.writeline "Function OpenFileDialog(StartPath, Filter, Caption)"
.writeline " OpenFileDialog = Dlg.openfiledlg(CStr(StartPath), ," _
& " CStr(Filter),CStr(Caption))"
.writeline "End Function"

.writeline "</SCRIPT><BODY><OBJECT id=WebBrowser "
.writeline "classid=clsid:8856F961-340A-11D0-A96B-00C04FD705A2>"
.writeline " <PARAM name=RegisterAsBrowser value=1 width=0
height=0>"
.writeline "</OBJECT><OBJECT id=Dlg"
.writeline " classid=CLSID:3050F4E1-98B5-11CF-BB82-00AA00BDCE0B"
.writeline " width=0 height=0></OBJECT></BODY></HTML>"
end with ' temp HTA file

' Launch the HTA and wait for it to load
oWS.Run sTemp, 1, False
Do : wsh.sleep 100 : Loop until oWS.AppActivate(sTitle)

' Find the HTA's window from the WebBrowser ID
For Each Wdw In CreateObject("Shell.Application").Windows
If Wdw.GetProperty("ID") = ID Then
set oWdw = Wdw.document.parentWindow
bFound = True
exit for
end if
Next
if not bFound then
wsh.echo "Not found."
ofs.DeleteFile sTemp
end if

MakeHTA = Array(oWdw, sTemp)

end Function ' MakeConsole

Caution: Watch for line wrapping. Few lines start in the first column
(comments, Function/End, etc.). All others have wrapped.
_____________________
Tom Lavedas

Mayayana

unread,
May 26, 2010, 1:21:14 PM5/26/10
to
your version works well, although I see a small title
bar above the dialogue. I was hesitant to use
that kind of approach because it seems like a security
bug to be able to run an HTA, so I thought that
MS might fix it someday. :)

I have another example here that's derived from
something I found online. I've tried it different
ways but this particular code seems to be the only
variant that works. The javascript in the MSHTA command
line does not seem to work if VBS is used instead.
I don't entirely understand how this is working. I
had no idea that the MSHTA command line was so
flexible, or that it was possible to get an HTA window
from Shell.Windows.

MsgBox FileOpen("C:\windows\notepad.exe", "GIF JPG BMP, gif jpg bmp, Text

Files, txt, HTML Files, htm html, All Files, *", "Choose File")

WScript.Quit

Function FileOpen(Path, Filter, Title)
Dim IE, HTA, ShAp


Dim A1, sFilter, i2, s1

CreateObject("WScript.Shell").Run "MSHTA.EXE ""javascript:new
ActiveXObject('InternetExplorer.Application').PutProperty('ID1',
window);""", 0
Set ShAp = CreateObject("Shell.Application")
WScript.sleep 300
On Error Resume Next
For Each IE In ShAp.Windows
If IsObject(IE.GetProperty("ID1")) Then
Set HTA = IE.GetProperty("ID1")
IE.quit
Exit For
End If
Next
Set ShAp = Nothing

A1 = Split(Filter, ",")

i2 = 0
Do While i2 < UBound(A1)

sFilter = sFilter & Trim(A1(i2))
s1 = Trim(A1(i2 + 1))

s1 = "*." & s1

s1 = Replace(s1, " ", ";*.")


sFilter = sFilter & "(" & s1 & ")|" & s1 & "|"

i2 = i2 + 2
Loop

HTA.document.body.innerHTML = "<OBJECT ID=Dlg
CLASSID=CLSID:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object>"
FileOpen = HTA.Dlg.object.openfiledlg(CStr(Path), , CStr(sFilter),
CStr(Title))
HTA.close
Set HTA = Nothing
End Function

-----------------------------------

Tom Lavedas

unread,
May 26, 2010, 1:59:31 PM5/26/10
to
{snip}

That IS interesting. It's creating the HTA 'on-the-fly' via the
command line. You say that you didn't know an HTA can be found using
the Shell.Application object - it can't. That's why the HTA creates
an instance of the WebBrowser object. In this approach, it is via the
IE.Application object being instantiate via the command line code. In
the one I posted, it was via the embedded WebBrowser object. I would
note that the 'javascript:function' is part of the W3 standard, while
there is no equivalent 'vbscript:function' implementation.

Yours is certainly trimmer than mine, doesn't require a temporary file
and doesn't have the distracting HTA title bar. I did have a problem
the first time I ran it in that the open loop Sleep was not long
enough for my system to load the HTA before it tested for it.
Subsequent executions did work, because it was in the cache.

All in all it seems a superior approach (except there doesn't seem to
be a way to position it on the screen).
_____________________
Tom Lavedas

Mayayana

unread,
May 26, 2010, 3:30:12 PM5/26/10
to

>
In
the one I posted, it was via the embedded WebBrowser object.
>

That's odd. A WB in an HTA. I'd be interested to
know about any other uses of that. I didn't realize
the WB object was available at all.

>
I would
note that the 'javascript:function' is part of the W3 standard, while
there is no equivalent 'vbscript:function' implementation.
>

I wondered about that. The VBS version seems
to work, but then the object is never found. Then
I thought that maybe the "window" reference wasn't
working from VBS. In a search I found examples
of vbscript: useage, but the syntax is difficult to
follow without some kind of guide. For instance,
this page:

http://www.ms-news.net/f2893/starting-four-executables-asynchronously-exiting-3673053.html

had this code:

mshta vbscript:CreateObject("Wscript.Shell").run("cmd.ex
e",0)(window.close)

>I did have a problem
the first time I ran it in that the open loop Sleep was not long
enough for my system to load the HTA before it tested for it.
>

Here's a different version that deals with that.
It will loop for a full second if necessary, but won't
stay in the loop if the HTA is not found:

iCount = 1
Do Until iCount = 10


For Each IE In ShAp.Windows
If IsObject(IE.GetProperty("ID1")) Then
Set HTA = IE.GetProperty("ID1")
IE.quit

Exit Do
End If
Next
WScript.sleep 100
iCount = iCount + 1
Loop

Stefan Kanthak

unread,
May 27, 2010, 5:02:32 AM5/27/10
to
"Tom Lavedas" <tglb...@verizon.net> wrote:

[...]

Nice.

You dont need to instanciate WScript.Shell and use
Environment("PROCESS").Item("TEMP"), just use FSO's GetSpecialFolder():

> ' Generate a temporary file name for the HTA
> set oWS = CreateObject("Wscript.Shell")
> stemp = oWS.Environment("PROCESS")("TEMP") & "\" _
> & ofs.GetTempName & ".hta"

stemp = ofs.GetSpecialFolder(2).Path & "\" _


Stefan

Dave "Crash" Dummy

unread,
May 27, 2010, 8:33:38 AM5/27/10
to
<snipping most of this to get to a single point.>

> I would note that the 'javascript:function' is part of the W3
> standard, while there is no equivalent 'vbscript:function'
> implementation.
>
> I wondered about that. The VBS version seems to work, but then the
> object is never found. Then I thought that maybe the "window"
> reference wasn't working from VBS. In a search I found examples of
> vbscript: useage, but the syntax is difficult to follow without some
> kind of guide. For instance, this page:

It doesn't seem to matter what scripting language you call with the
"javascript:" prefix. It seems to be a sort of generic "script call"
label. I use "onClick='javascript:vbsub()'" regularly. You can
even use the JS "this" object to pass variables:
"onClick='javascript:vbsub(this.value)'", which I find *very* handy!

Mayayana

unread,
May 27, 2010, 9:04:19 AM5/27/10
to

--
--
"Dave "Crash" Dummy" <inv...@invalid.invalid> wrote in message
news:wEtLn.39125$_84....@newsfe18.iad...

Do you know anything about the syntax in the
sample I found?

mshta vbscript:CreateObject("Wscript.Shell").run("cmd.ex
e",0)(window.close)

The window.close part seems to imply that parentheses
are acting to define a new line, but that's not normal VBS.
And when I tried using a colon for new line in a single
command line, it didn't seem to work. I'm wondering if
there might be an entire, unique syntax system for these
calls.


Dave "Crash" Dummy

unread,
May 27, 2010, 10:09:34 AM5/27/10
to
Mayayana wrote:
> "Dave "Crash" Dummy" <inv...@invalid.invalid> wrote in message
> news:wEtLn.39125$_84....@newsfe18.iad...
> | <snipping most of this to get to a single point.>
> |
> | > I would note that the 'javascript:function' is part of the W3
> | > standard, while there is no equivalent 'vbscript:function'
> | > implementation.
> | >
> | > I wondered about that. The VBS version seems to work, but then the
> | > object is never found. Then I thought that maybe the "window"
> | > reference wasn't working from VBS. In a search I found examples of
> | > vbscript: useage, but the syntax is difficult to follow without some
> | > kind of guide. For instance, this page:
> |
> | It doesn't seem to matter what scripting language you call with the
> | "javascript:" prefix. It seems to be a sort of generic "script call"
> | label. I use "onClick='javascript:vbsub()'" regularly. You can
> | even use the JS "this" object to pass variables:
> | "onClick='javascript:vbsub(this.value)'", which I find *very* handy!
> | --
>
> Do you know anything about the syntax in the
> sample I found?
>
> mshta vbscript:CreateObject("Wscript.Shell").run("cmd.exe",0)(window.close)

>
> The window.close part seems to imply that parentheses
> are acting to define a new line, but that's not normal VBS.
> And when I tried using a colon for new line in a single
> command line, it didn't seem to work. I'm wondering if
> there might be an entire, unique syntax system for these
> calls.

No. I was talking about script calls from HTML, exclusively. I don't
understand that command line string at all, but in my experience with
window.close, empty parentheses "window.close()" are required.
--
Crash

Morals allow predatory animals to live in large herds.

Tom Lavedas

unread,
May 27, 2010, 11:04:03 AM5/27/10
to
On May 27, 5:02 am, "Stefan Kanthak" <postmaster@[127.0.0.1]> wrote:

Interesting. I had never seen that function. Learned something new.
It is clearly an appropriate solution. However, I point out that my
approach used the Shell object later to launch the HTA. This whole
thing is made moot by Mayayana's solution using the the mshta.exe
command line approach for creating the HTA.
_____________________
Tom Lavedas

Mayayana

unread,
May 27, 2010, 11:27:06 AM5/27/10
to
> stemp = ofs.GetSpecialFolder(2).Path & "\" _
>
Interesting. I had never seen that function.
-------------

I actually treat that as a string function:

s = FSO.GetSpecialFolder(0) 'windows
s = FSO.GetSpecialFolder(1) 'system
s = FSO.GetSpecialFolder(2) 'temp

I think it works because Path is the default
property. If one uses Set the folder object is
returned. If one uses a non-object variable
the path string is returned.


angelo tavola

unread,
Feb 13, 2023, 12:29:21 PM2/13/23
to
Il giorno sabato 22 maggio 2010 alle 13:51:28 UTC+2 Dave "Crash" Dummy ha scritto:
> I have a script function that I used successfully in Windows 2000, but
> does not work fully in Windows 7 (x64). The test script below works fine
> at returning folder paths and file paths for files with .htm/.html
> extensions. It fails for files with any other extension (.txt, .hta,
> .vbs tried). As configured, the function returns an error number of 424
> when it malfunctions. If "On Error Resume Next" is removed, the
> malfunction generates a popup specifiying an unknown error number
> 80004000 for the line containing the BrowseForFolder method.
> I'd sure like to use this or a similar script to select ALL file types.
> '================= getFile.vbs ==========================
> msgbox getFile()
> function getFile()
> Set objShell = CreateObject("Shell.Application")
> on error resume next
> Set objFolder = objShell.BrowseForFolder(0, "Select an author:",
> &H4000& , "e:\books")
> Set objFolderItem = objFolder.Self
> if err.number = 0 then
> getFile=objFolderItem.path
> else
> getFile=err.number
> err.clear
> end if
> end function
> '=====================================================
> --
> Crash
> "The real question is not whether machines think but whether men do."
> ~ B. F. Skinner ~
0 new messages