User Interaction with Common Dialogs in WSH - Accessing the Common Dialog control directly
(Page 2 of 4 )
Using the Microsoft Common Dialogs control is fairly straightforward. You initialize the object by connecting to its namespace, assign the dialog's attributes, and then call its display method. Here's a quick example that opens the familiar Open File dialog.
Set objDlgs = CreateObject("MSComDlg.CommonDialog")
objDlgs.Filter = "All Files(*.*)|*.*|VBScript Files(*.vbs)|*.vbs"
objDlgs.FilterIndex = 2
objDlgs.MaxFileSize = 128
objDlgs.ShowOpen()
WScript.Echo objDlgs.FileName
The Filter property tells the dialog box what to offer in the file types drop-down box. The FilterIndex property sets the default.
You must use the MaxFileSize to set the maximum name length or the control will generate an error.
Finally, a call to the ShowOpen method returns a string containing the file name and path. And empty string is returned if the user presses the Cancel button.
Set objDlgs = CreateObject("MSComDlg.CommonDialog")
objDlgs.Filter = "Text Files(*.txt)|*.txt"
objDlgs.FilterIndex = 1
objDlgs.MaxFileSize = 128
objDlgs.ShowSave()
WScript.Echo objDlgs.FileName
The Save dialog works in the exact same way. The code above opens a Save dialog with the default file type set to Text Files.
The Common Dialogs control offers many more familiar dialogs that you can implement in your scripts. Among these are Choose a Color, Choose a Font, and the Print dialog.
So why am I touching on these so briefly? Well, because this probably will not work for most of you. While the Common Dialogs control is installed on every Windows system by default, it is a licensed control. That means that its functionality is based upon which license is installed on the machine.
There are two types of licenses: runtime and development. A runtime license (the default) only allows the common dialogs to be called from software that was written with a development license. As you can probably guess, to use this control in WSH you would need to have a development license installed on your machine.
A development license is installed by Visual Basic 6.0, Visual Studio, or Microsoft Office Developer Edition. So you can only use this control on any machine that has one of those titles installed. Since this rules out most scenarios, I didn't feel there was much point in spending a lot of time on this.
Let's take a look at some of the workarounds available that can be used as alternatives to the Common Dialog control.
Next: Accessing dialogs through other applications >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer