VBScript file open dialog that works in XP and Vista?
In XP, you can use VBScript with the UserAccounts.CommonDialog object to bring up a File Open dialog (as described here), but apparently this does not work under Vista.
Is there a VBScript method for File-Open dialogs that开发者_高级运维 will work for both?
Or even one that will work nicely for Vista?
Disclaimer: I'm a proper programmer, honest, and don't usually work with VBScript - I am asking this question 'for a friend'.
You can create a simple dot net component that exposes a COM interface, so you can use it in VBScript (or any COM/ActiveX based technology).
- (1) Create a dot net library type project, expose the classes you want to be COM interoperability (adding ComVisible and ClassInterface attributes). The ClassInterface attribute must be set to AutoDual so you can create instances by late binding.
- (2) mark the register for COM interoperability check-box in the build tab from the project properties dialog.
- (3) build the project, so the component can be properly registered (you have the option to create a setup project for your component so it can be easily deployed).
...
namespace WinUtility
{
[ComVisible(true), Guid("32284FD3-417E-45fc-A4A0-9344C489053B"),
ClassInterface(ClassInterfaceType.AutoDual)]
public class WinDialog
{
public string ShowOpenFileDialog()
{
string result = string.Empty;
OpenFileDialog d = new OpenFileDialog();
if (d.ShowDialog() == DialogResult.OK) { result = d.FileName; }
return result;
}
}
}
Once your component is registered you can instantiate it from VBScript:
dim wnd_helper, file_name
Set wnd_helper = CreateObject("WinUtility.WinDialog")
file_name = wnd_helper.ShowOpenFileDialog()
if trim(file_name) <> "" then
msgbox "file: " + file_name
else
msgbox "No file selected."
end if
精彩评论