Workarounds for Common Dialogs in WSH

In my last article we learned how to use Microsoft’s Common Dialogs control to launch the File Open and File Save dialog boxes for grabbing user input. We also learned that this will not work in many cases. In this article we explore some alternative methods.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
April 25, 2007
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

If you missed the first part of this series, you should stop now and read it here.

The first workaround for the Common Dialog control’s Open dialog is a very simple one.  It’s provided by the User Account control and works nearly identically to the original Common Dialog.

Const WORD_DOCS = "Word Documents (*.doc)|*.doc|"

Const ALL_FILES = "All Files (*.*)|*.*|"

Set objDialog = CreateObject("UserAccounts.CommonDialog")

objDialog.Filter = WORD_DOCS & ALL_FILES

objDialog.FilterIndex = 1

objDialog.InitialDir = "%homepath%\My Documents"

intResult = objDialog.ShowOpen

If intResult Then

   Wscript.Echo objDialog.FileName

Else

   Wscript.Echo ""

End If

As you can see, this works in very much the same way.  The Filter and FilterIndex properties are used to set the default file types for the control and the ShowOpen method is used to launch it.

Just as with Common Dialogs each file type filter requires two parts: the text description and the filter.  Parts are combined into a single piped string.

Pipe is another name for the (|) character.  That’s the 124th ASCII character: that’s 124 in decimal notation, 0x7C in hex, and 0174 in octal.

The two things to note when using this method are the use of the InitialDir property to specify the directory that the dialog should open to and the use of an If statement for parsing its return value.  If the user clicks the Cancel button, the dialog returns Nothing.

Okay, so what else can we do to work around not being able to use Microsoft’s Common Dialogs control?

Useful alternatives

You probably remember me stating that the Microsoft’s Common Dialogs control is a licensed control and cannot be used on machines that do not have a Development license installed.  Since most don’t, we’re forced to find a workaround.

You should also remember me saying that programs created on these machines can use the control.  That’s leaves us one more possible workaround—using a third-party program or control that was developed on a machine with a developer license.

There are a few of these available.  Most of them are ActiveX controls that work as wrappers.  A wrapper simply takes a request and passes it on to the Common Dialog control on our behalf.

The down side to this approach is that you will need to distribute the third-party control with your script and it will need to be installed on each machine as well.  Installation is as simple as a service registration, but it’s still a hassle.

JSWare has released several freeware controls that you may find useful.  Many of them also include a Common Dialogs wrapper.

In this example, I’m going to use JSWare’s JSDlgBox.dll.  Once you download and register it, you can access it like any other COM object with its ProgID.

Set objDialogs = CreateObject("JSDlgBox.Browser")

WScript.Echo objDialogs.OpenBox

The syntax is very simple.  After connecting to the JSDlgBox object, you only need to call the function for whichever dialog you wish to display.  No parameters are necessary.

Set objDialogs = CreateObject("JSDlgBox.Browser")

WScript.Echo objDialogs.SaveBox

Both functions return empty strings if the user presses the Cancel button, and both also use the standard All Files filter.

While it is slightly restrictive when it comes to providing control over the dialogs, the JSDlgBox control does provide a very easy method of accessing the Common Dialogs control.

The Browse For Folder dialog

The last workaround I’d like to share with you is technically not a workaround since it’s not a Common Dialog.  However, it is a standard Windows dialog and can be very useful.  It’s the Browse For Folder dialog.

Let me preface myself by saying that the Browse For Folder dialog is a bit complex.  I’ll provide the basics here to get it working, but the best way to learn it is to try it for yourself.

The Browse For Folder dialog uses a lot of preset values that determine its appearance and functionality.  It’s beyond the scope of this article for me to get into each of them.  You can find more information here.

You can include only those constants you require or the whole list.  In either case, here they are.

'BrowseForFolders hOptions - accepts multiple

Const NO_OPTIONS = &H0000

Const FIND_FOLDER = &H0001

Const FIND_COMPUTER = &H0002

Const SHOW_STATUS = &H0004

Const RETURN_ANCESTORS = &H0008

Const SHOW_EDITBOX = &H0010

Const VALIDATION = &H0020

Const NEW_STYLE = &H0040

Const BROWSE_FOR_COMPUTERS = &H1000

Const BROWSE_FOR_PRINTER = &H2000

Const BROWSE_EVERYTHING = &H4000

 

'BrowseForFolders intRootFolder

Const DESKTOP = 0

Const PROGRAMS = 2

Const CONTROLS = 3

Const PRINTERS = 4

Const PERSONAL = 5

Const FAVORITES = 6

Const STARTUP = 7

Const RECENT = 8

Const SENDTO = 9

Const BITBUCKET = 10         ' Recycle Bin

Const STARTMENU = 11

Const DESKTOPDIRECTORY = 16  ' (&H10)

Const DRIVES = 17            ' (&H11) MyComputer Drives

Const NETWORK = 18           ' (&H12)

Const NETHOOD = 19           ' (&H13)

Const FONTS = 20             ' (&H14)

Const TEMPLATES = 21         ' (&H15)

Const COMMONSTARTMENU = 22   ' (&H16)

Const COMMONPROGRAMS = 23    ' (&H17)

Const COMMONSTARTUP = 24     ' (&H18)

Const COMMONDESKTOPDIR = 25  ' (&H19)

Const APPDATA = 26           ' (&H1A)

Const PRINTHOOD = 27         ' (&H1B)

Const LOCALAPPDATA = 28      ' (&H1C)

Const ALTSTARTUP = 29        ' (&H1D)

Const COMMONALTSTARTUP = 30  ' (&H1E)

Const COMMONFAVORITES = 31   ' (&H1F)

Const INTERNETCACHE = 32     ' (&H20)

Const COOKIES = 33           ' (&H21)

Const HISTORY = 34           ' (&H22)

Const COMMONAPPDATA = 35     ' (&H23)

Const WINDOWS = 36           ' (&H24)

Const SYSTEM = 37            ' (&H25)

Const PROGRAMFILES = 38      ' (&H26)

Const MYPICTURES = 39        ' (&H27)

Const PROFILE = 40           ' (&H28)

Const SYSTEMx86 = 41         ' (&H29)

Const PROGRAMFILESx86 = 48   ' (&H30)

Using the Browse For Folder dialog

The Browse For Folder dialog is a part of Internet Explorer (shdocvw.dll) but is accessed through the Windows Shell.  So after defining your constants, you’ll need to connect to Shell.Application.

Const WINDOW_HANDLE = 0

Const SHOW_EDITBOX = &H0010

Const VALIDATION = &H0020

Const DESKTOP = 0

Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, _

   "Please select a folder", _

   SHOW_EDITBOX + VALIDATION, DESKTOP)

If objFolder Is Nothing Then

   WScript.Echo ""

Else

   WScript.Echo objFolder.Self.Path

End If

Here you can see the Browse For Folder dialog in action.  After connecting to the Shell object, call its BrowseForFolder method to open the dialog.  Now, here’s where it gets a little tricky.  There are a lot of parameters to contend with.

The first parameter is used to attach the dialog to a specific window handle.  This is not possible in WSH so this value will always be set to 0.

The second option is the text prompt that appears as the dialog box’s caption.  It accepts a simple text string.

The third parameter sets the style and functionality of the box.  It can accept a combination of preset hex values.  Remember that hex values are a numeric type and should be combined using a plus sign.

In the example above, I’ve instructed the box to show an edit box where the user can manually enter a path.  I’ve also included the VALIDATE option which forces the dialog box to validate any entry before allowing the user to click the OK button.

The final parameter is used to set what folder is displayed when the dialog box opens.  Be very careful when setting this folder.  The user will not be able to browse above this directory.

Once you have all of the pieces in place you’re all set.  If the user presses the Cancel button, the dialog returns Nothing.  In any other case it returns the folder’s shell namespace object.

Finally you can access the Self sub-object and use its Path property to return a string.

I’ve given you a lot of different ways to add dialogs to your scripts.  Choose the method or methods that suit your needs the best based on ease of use and flexibility.  Play around with all of them and see what they have to offer.  Until next time, keep coding!

blog comments powered by Disqus
WINDOWS SCRIPTING ARTICLES

- More Windows Scripting Workarounds from Nilpo
- Overloading Methods and More in VBScript
- Improving MFC for Windows Vista
- Regular Expressions in VBScript
- Working with Dates in WMI
- Completing Calendars with VBScript Date Func...
- Building Calendars with VBScript Date Functi...
- Working With Dates and Times in VBScript
- Designing WCF DataContract Classes Using the...
- Understanding Dates and Times in VBScript
- Working With Arrays in VBScript
- Compressed Folders in WSH
- Using .NET Interops in VBScript
- Nilpo`s Scripting Secrets, Vol I
- Database operations using Silverlight 2.0 WC...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials