Workarounds for Common Dialogs in WSH
(Page 1 of 4 )
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?
Next: Useful alternatives >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer