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

Open File Dialog

339 views
Skip to first unread message

Marcus Moehrmann

unread,
Aug 18, 2002, 4:56:09 AM8/18/02
to
Hello

how to call the Open File Dialog from .VBS, let the user select a file and
return the full file name to .VBS ?

Regards Marcus


Claus Andersen

unread,
Aug 18, 2002, 12:12:34 PM8/18/02
to
"Marcus Moehrmann" <mar...@typolog.de> wrote in
news:3d5f6...@news.arcor-ip.de:

> how to call the Open File Dialog from .VBS, let the user select a file
> and return the full file name to .VBS ?

There is no nice GUI for fileselection built into VBscript. If you have
Microsoft Excel installed you can however use that behind the back of your
user to get the dialog you need. The following code sniplet will write the
name of the file you select to the console if Excel is installed:
____________________________

Option Explicit

Dim oExcel
Set oExcel = CreateObject("Excel.Application")
WScript.Echo oExcel.GetOpenFilename()
____________________________

GetOpenFilename is documented here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/wmisdk/wmi/getting_management_information_from_an_existing_application.a
sp

--
Claus Andersen, cl...@wheel.dk
"Rosebud"

Torgeir Bakken

unread,
Aug 18, 2002, 12:10:37 PM8/18/02
to
Marcus Moehrmann wrote:

> how to call the Open File Dialog from .VBS, let the user select a file and
> return the full file name to .VBS ?

Hi

Go here
http://home.att.net/~wshvbs/index.htm

and search for
Using the system "Open" and "SaveAs" Common Dialogs...


--
torgeir


Gary

unread,
Aug 19, 2002, 10:01:17 PM8/19/02
to
Hi Marcus,

VBScript itself doesn't provide any UI for use. However, we can invoke
other component to implement the same task.

In this case, we can create an instance of the MSComDlg.CommonDialog
(comdlg32.ocx). Below is the code snippet:

Option Explicit

' Windows Common Dialogue Error Constant:
' The selected file must exist, non-existent file entry
' will prompt for existing file
Const cdlOFNFileMustExist = &H1000

Dim oDlg1, sFileName

' selecting the Cancel button generates an error so disable
' error notification
On Error Resume Next

' create a common dialog object
Set oDlg1 = wscript.CreateObject("MSComDlg.CommonDialog")

' set the dialog's properties
With oDlg1
.DialogTitle = "Select the script you want to open..."
.Filter = "JScript Files (*.js)|*.js|VBScript Files(*.vbs)|*.vbs"
.FilterIndex = 2
.MaxFileSize = 260
.Flags = cdlOFNFileMustExist
' display the Open File Dialog
.ShowOpen
'assign the filename selected to a variable for validation**
sFileName = .FileName
End With

If sFileName <> "" Then
WScript.Echo sFileName
End If

' exit the script
WScript.Quit

To make the code above run, you should make sure that the comdlg32.ocx
exists on the machine. The comdlg32.ocx was shipped with many products.
Please check it at
http://support.microsoft.com/default.aspx?scid=/servicedesks/fileversion/dll
info.asp&fp=1.

Gary

This posting is provided "AS IS", with no warranties, and confers no
rights. Enjoyed ASP.NET? http://www.asp.net

mayayana

unread,
Aug 20, 2002, 10:02:00 AM8/20/02
to
You can only use the MS ComDlg if it's licensed on the system
where it's called.

1) If you call:

Set shellapp = createobject("Shell.Application")
Set FolOrFil = shellapp.BrowseForFolder(0, "Select file.", 16384)

you'll show the BrowseForFolder dialogue with files showing,
but it's a pain to extract the path. FolOrFil will return only the file
(or folder) name selected.

2) You can also tap into the IE file browsing ability to show an
IE window with a browse button that will return the path selected.

3) Use a component.

For samples of 1 and 2:
http://www.jsware.net/jsware/scripts.html (ShellApp scripts)

For #3 scroll down to the components section.


--
--
Marcus Moehrmann <mar...@typolog.de> wrote in message
news:3d5f6...@news.arcor-ip.de...

Michel Gallant

unread,
Aug 20, 2002, 10:19:48 AM8/20/02
to
There are some excellent examples, and good discussion of BrowseForFolder()
and related in Gunter Born's book:

Windows Script Host 2 Developers Guide - MSPress 2000

- Mitch Gallant
http://home.istar.ca/~neutron/wsh

0 new messages