4

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'.

codeulike
  • 22,514
  • 29
  • 120
  • 167
  • possible duplicate of http://stackoverflow.com/questions/401257/com-object-to-create-a-open-file-dialog-under-vista – Helen Apr 15 '10 at 17:20
  • 1
    +1 for hilarious disclaimer. Your 'friend' sounds a lot like a 'friend' of mine. Maybe there should be a support group? – dmb Apr 16 '10 at 05:16
  • @Dave Turvey: No, I gave up on it. Wait ... I mean my friend gave up on it. – codeulike Aug 10 '10 at 15:09

1 Answers1

1

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
ArBR
  • 4,032
  • 2
  • 23
  • 29