Search code examples
vbavbscriptfilepath

VBScript to open a dialog to select a filepath


At present I am opening a file with my vbscript as follows:

strFile = "C:\Users\test\file.txt"
Set objFile = objFSO.OpenTextFile(strFile)

I would like to change this so that a file can be selected/navigated to by the user and that file is used in the script. How can I add this ability? I have tried to search for how to load a file dialog/prompt the user for a file etc just not sure how to complete in a VBScript.


Solution

  • There is another solution I found interesting from MS TechNet less customization but gets what you wanted to achieve. This returns the full path of the selected file.

    Set wShell=CreateObject("WScript.Shell")
    Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
    sFileSelected = oExec.StdOut.ReadLine
    wscript.echo sFileSelected