User Interaction with Common Dialogs in WSH - Accessing dialogs through other applications
(Page 3 of 4 )
If you can't call the Common Dialog control directly, it makes sense to try and call it from an application that can. However, this seems like a bit of a stretch. The Common Dialog control would be called within the code of the application so we would need to find a way to trap the results it returns.
To do that, we'll look to Internet Explorer. If you create a web page with a Browse form button, clicking the button will launch the File Open dialog box. The results of that dialog are then returned to the button's accompanying text box.
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
Do Until objIE.ReadyState = 4
WScript.Sleep 1
Loop
objIE.Document.Write("<html><body><input id=field " _
& "type=file></body></html>")
objIE.Document.all.field.focus
objIE.Document.all.field.click
WScript.Echo objIE.Document.all.field.value
objIE.Quit
This example goes a little further than the VBS scripting you're probably used to because we're also implementing some browser scripting as well.
Browser scripting relies on the Document Object Model (DOM) for controlling web page elements. In this case, we're using client-side scripting to control a web form. The web page itself was also created with the DOM.
We begin by loading a blank HTML page in Internet Explorer using the IE object's Navigate method. Then we write an HTML string to the page using the Write method of DOM's Document object.
We make sure that the form field is in focus and then simulate a mouse click on the Browse button. This brings up the Open file dialog box. Once the user makes a selection, the value is returned to the form field.
We finish up by reading the field's contents with the Value property. Make sure to use IE's Quit method to close the application when you're finished. Otherwise, the process will remain running in the background.
So why don't I like this method? Well, first off it does not give you any control over the dialog box's appearance or functionality. It's very cut-and-dry. Second, I don't like the idea of having to launch another application if I can help it.
This does work as a viable alternative to the Common Dialogs control, however, because it's a pretty safe bet that IE will be installed on every machine.
The final reason I don't like this approach is because you only have access to a File Open dialog. What if I want to save a file? Technically, I could still use this since we're only returning a string but that could be confusing to our users.
Next: Watch it in action >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer