3

I found this function on GitHub and it's working great for what I need, but I can't figure out how to specify file type. It seems to default to "all".

Function BrowseForFile()
    With CreateObject("WScript.Shell")
        Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
        Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
        Dim tempName : tempName = fso.GetTempName() & ".hta"
        Dim path : path = "HKCU\Volatile Environment\MsgResp"
        With tempFolder.CreateTextFile(tempName)
            .Write "<input type=file name=f><script>f.click()" & _
            ";(new ActiveXObject('WScript.Shell'))" & _
            ".RegWrite('HKCU\\Volatile Environment\\MsgResp', f.value)" & _
            ";close();<" & "/" & "script>"
            .Close
        End With
        .Run tempFolder & "\" & tempName, 1, True
        BrowseForFile = .RegRead(path)
        .RegDelete path
        fso.DeleteFile tempFolder & "\" & tempName
    End With
End Function

'Example: msgbox BrowseForFile()

I tried modifying this line:

.Write "<input type=file name=f ><script>f.click()" & _
";(new ActiveXObject('WScript.Shell'))" & _
".RegWrite('HKCU\\Volatile Environment\\MsgResp', f.value)" & _
";close();<" & "/" & "script>"

To this:

.Write "<input type=file accept=" & chr(34) & "text/plain" & chr(34) & " name=f ><script>f.click()" & _
";(new ActiveXObject('WScript.Shell'))" & _
".RegWrite('HKCU\\Volatile Environment\\MsgResp', f.value)" & _
";close();<" & "/" & "script>"

But, still not working. Is there any way to use this function and customize the allowed file types? I want to allow only .csv, .txt, and .log

Drivium
  • 537
  • 6
  • 24
  • Aside: What are you trying to accomplish here? VBScript cannot be used reliably on web pages (and won't work at all on MSIE11+), and Visual Basic as a whole is on its way out. –  Jul 29 '16 at 01:48
  • I'm aware. I mainly like how easy it's been to manipulate IE with, but I'm aware I need to let go... What do you recommend as a replacement? – Drivium Jul 30 '16 at 02:18

1 Answers1

3

I used this function here : How to add filter to a file chooser in batch?

Function GetFileDlgEx(sIniDir,sFilter,sTitle) 
Set oDlg = CreateObject("WScript.Shell").Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);eval(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0).Read("&Len(sIniDir)+Len(sFilter)+Len(sTitle)+41&"));function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg(iniDir,null,filter,title)));close();}</script><hta:application showintaskbar=no />""") 
oDlg.StdIn.Write "var iniDir='" & sIniDir & "';var filter='" & sFilter & "';var title='" & sTitle & "';" 
GetFileDlgEx = oDlg.StdOut.ReadAll 
End Function
set fso = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = fso.GetAbsolutePathName(".")
sIniDir = CurrentDirectory &"\Myfile.csv" 
sFilter = "csv files (*.csv)|*.csv|txt files (*.txt)|*.txt|log files (*.log)|*.log|" 
sTitle = "Put a title of your program here :) (-_°)" 
MyFile = GetFileDlgEx(Replace(sIniDir,"\","\\"),sFilter,sTitle) 
wscript.echo MyFile
Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Holy hell - this function is awesome. Thank you for this! Even BETTER than what I had. Far more customizable. Cheers! – Drivium Aug 03 '16 at 15:34
  • So, I'm aware of the traditional .BrowseForFolder method, but since I like the customization possible with this script such as the title bar and how it uses the traditional browse dialogue box, how might I modify this to browse just for a directory? In other words, leaving every other aspect of this script in tact, simply browsing for a directory instead of a file. Possible? – Drivium Aug 04 '16 at 18:34
  • Instead of the `object` tags, I use: `about:` so maybe `folder` instead of `file`. The file filter will not be needed. – June7 Mar 03 '19 at 08:39