Workarounds for Common Dialogs in WSH - Using the Browse For Folder dialog
(Page 4 of 4 )
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!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |