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.
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?
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 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.
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!