locked
windows7 VBScript open file dialog box - fakepath RRS feed

  • Question

  • is there any solution in vbs to handle c:\fakepath?

    Windows7 x86

     

    code:

     

    Option Explicit
     
     Function ChooseFile()
         Dim Result
         Dim IE : Set IE = CreateObject("InternetExplorer.Application")
         With IE
             .Visible = False
             .Navigate("about:blank")
             Do Until .ReadyState = 4 : Loop
             With .Document
                 .Write "<html><body><input id='f' type='file'></body></html>"
                 With .All.f
                     .Focus
                     .Click
                     Result = .Value
                 End With
             End With
             .Quit
         End With
         Set IE = Nothing
        ChooseFile = Result
     End Function
     
     MsgBox ChooseFile()

    Monday, July 19, 2010 8:56 PM

Answers

  • This is most likely your problem. The following text can be found here.

    From MSDN

    "Internet Explorer 8 and later. When a file is selected by using the input type=file object, the value of the value property depends on the value of the "Include local directory path when uploading files to a server" security setting for the security zone used to display the Web page containing the input object. For more information value."

    You can change this setting on all of your machines or you may need to think about creating your application as a HTA.


    v/r LikeToCode....Mark the best replies as answers.
    • Proposed as answer by LikeToCode Sunday, August 1, 2010 1:13 AM
    • Marked as answer by Boe ProxMVP Saturday, September 11, 2010 2:41 AM
    Friday, July 23, 2010 4:09 AM

All replies

  • Have you tried this? Ensure that mscomdlg32ocx is registered for this to work.

    Dim objDialog
    Set objDialog = CreateObject("MSComDlg.CommonDialog")   
    With objDialog
    	.Filter = "*.txt"   
    	.InitDir = "C:"   
    	.MaxFileSize = 256   
    	.Flags = &H80000 + &H4 + &H8 
    End With  
      
    If objDialog.ShowOpen = False Then
    	WScript.Echo "You pressed 'Cancel' please try again."
     	Wscript.Quit
    Else
    	WScript.Echo objDialog.FileName
    End If
    

    v/r LikeToCode....Mark the best replies as answers.
    • Proposed as answer by LikeToCode Sunday, August 1, 2010 1:13 AM
    Tuesday, July 20, 2010 12:12 AM
  • I download the mscomdlg32.ocx,but failed to register in window 7.

    Any helps?

    Tuesday, July 20, 2010 5:05 AM
  • same here..., need to run this script on XP and VIsta and Windows 7. With XP is no problem but Vista/7....
    Tuesday, July 20, 2010 12:50 PM
  • This is most likely your problem. The following text can be found here.

    From MSDN

    "Internet Explorer 8 and later. When a file is selected by using the input type=file object, the value of the value property depends on the value of the "Include local directory path when uploading files to a server" security setting for the security zone used to display the Web page containing the input object. For more information value."

    You can change this setting on all of your machines or you may need to think about creating your application as a HTA.


    v/r LikeToCode....Mark the best replies as answers.
    • Proposed as answer by LikeToCode Sunday, August 1, 2010 1:13 AM
    • Marked as answer by Boe ProxMVP Saturday, September 11, 2010 2:41 AM
    Friday, July 23, 2010 4:09 AM
  • Um...okay.

    So I've got this VB saved to a file share on our network and I'm browsing to the share and double-clicking the .vbs file. If I change the Internet zone settings, it works like a champ (giving me the correct path).  However, that seems like quite a security risk to just open for every site.

    I guess I'll try to make my script (a user creation script that reads from an .xls file) into an HTA.  

    Monday, July 26, 2010 9:43 PM
  • Hi LikeToCode,

    My problem is that the "Active Object can't create object:MSComDlg.CommonDialog" when I run your vbs .

    And I have tried to register the  mscomdlg32.ocx to system(Windows 7), but failed.

    Could you help me?

     

    Wednesday, July 28, 2010 8:29 AM
  • gittywang,

    This is a scripting forum and does not deal with OS issues, like the one you are having. I would suggest you post here or here for a more accurate response. Just a thought ensure you run the cmd prompt with the "Run As Administrator" option before issue the register commands.


    v/r LikeToCode....Mark the best replies as answers.
    Wednesday, July 28, 2010 2:44 PM
  • Hi LikeTocode,

    I think that if your vbs can work, you also should register the mscomdlg32.ocx first, or you can't create the object of "MSComDlg.CommonDialog", is that right?

    So,could you tell me how can you let your vbs work?

    By the way, I'm sure I run the cmd with "Run as administrator". And the system is Windows7 64bit.

    I will be very appreciate if you can help me.

     

    Saturday, July 31, 2010 12:56 AM
  • I think that if your vbs can work, you also should register the mscomdlg32.ocx first, or you can't create the object of "MSComDlg.CommonDialog", is that right? 

    To register the .ocx try this: http://social.technet.microsoft.com/Forums/en-US/w7itproinstall/thread/8bb8f428-3c43-4b01-a909-480e265dacf2

    By the way, I'm sure I run the cmd with "Run as administrator". And the system is Windows7 64bit.

    Read this: http://www.sevenforums.com/tutorials/11841-run-administrator.html


    v/r LikeToCode....Mark the best replies as answers.
    Saturday, July 31, 2010 4:40 AM
  • Hi LikeToCode,

    Thank you for your information.

    Now I can register the .ocx succeded.

    And I run the vbs you write again after a system restart .

    But the result is still fail :(

    There is still a Error dialog pop up when run the vbs, and the error message is like "AcitveX component can't create object:'MSComDlg.CommonDialog.1'"

    And I check the ClassID of 'MSComDlg.CommonDialog.1' is exist in registry, which is HKEY_CLASSES_ROOT\MSComDlg.CommonDialog.1\CLSID\(default) = {F9043C85-F6F2-101A-A3C9-08002B2F49FB}

    Is there anything I lost?

     

    Tuesday, August 3, 2010 9:16 AM
  • Hi LikeToCode,

    Thank you for your information.

    Now I can register the .ocx succeded.

    And I run the vbs you write again after a system restart .

    But the result is still fail :(

    There is still a Error dialog pop up when run the vbs, and the error message is like "AcitveX component can't create object:'MSComDlg.CommonDialog.1'"

    And I check the ClassID of 'MSComDlg.CommonDialog.1' is exist in registry, which is HKEY_CLASSES_ROOT\MSComDlg.CommonDialog.1\CLSID\(default) = {F9043C85-F6F2-101A-A3C9-08002B2F49FB}

    Is there anything I lost?

     

    All,

    I also face the same problem,

    i have run regsvr32, and "succeded"

    but when i run the script, the error message still the same

     

    any other thing i have to do?

     

    cheers,

    jik

    Sunday, February 6, 2011 4:56 PM
  • Hi,

    I know this is an old topic, but if people stuggled with it google for it (I mean Bing for it!) they might be happy to find something...

    So here is the thing: this fakepath problem doesn't occure in HTA... ok, I know we want it to work from a vbs, not hta.

    So the idea is to create an hta file from vbscript, run it, hta would put the file path in a text file, then you retrieve the path in vbscript from that txt file.

    A bit crazy, I know, but I was inspired by a similar script that was writting a vbs file from HTA to have the Sleep function available.

    Here it is (the code is a bit long, but it works):

     Option Explicit

    WScript.Echo "Selected file: " & ChooseFile( )

    Function ChooseFile( )
     Dim objFSO, objShell, objTempFolder, strTempFileName, strFullTempFileName, objOpenFile, objTextFile, strTempTextFileName
     Const TemporaryFolder = 2
     Const ForReading = 1
     strTempFileName = "OpenFile.hta"
     strTempTextFileName = "OpenFile.txt"
     Set objFSO= CreateObject("Scripting.FileSystemObject")
     Set objTempFolder = objFSO.GetSpecialFolder(TemporaryFolder)
     strFullTempFileName=objTempFolder.Path & "\" & strTempFileName
     Set objOpenFile = objFSO.CreateTextFile(strFullTempFileName,True)
     objOpenFile.writeline("<html><head><meta http-equiv=""Content-Type"" content=""text/html; charset=windows-1252"">")
     objOpenFile.writeline("<title>Open File</title>")
     objOpenFile.writeline("<script language=""vbscript"">")
     objOpenFile.writeline("Sub Window_Onload")
     objOpenFile.writeline("FileName.click")
     objOpenFile.writeline("WriteFile FileName.value")
     objOpenFile.writeline("Self.Close()")
     objOpenFile.writeline("End Sub")
     objOpenFile.writeline("Sub WriteFile(strFileName)")
     objOpenFile.writeline("Dim objFSO, objTempFolder, strTempFileName, strFullTempFileName, objOpenFile")
     objOpenFile.writeline("Const TemporaryFolder = 2")
     objOpenFile.writeline("strTempFileName = ""OpenFile.txt""")
     objOpenFile.writeline("Set objFSO=CreateObject(""Scripting.FileSystemObject"")")
     objOpenFile.writeline("Set objTempFolder = objFSO.GetSpecialFolder(TemporaryFolder)")
     objOpenFile.writeline("strFullTempFileName=objTempFolder.Path & ""\"" & strTempFileName")
     objOpenFile.writeline("Set objOpenFile = objFSO.CreateTextFile(strFullTempFileName,True)")
     objOpenFile.writeline("objOpenFile.writeline(strFileName)")
     objOpenFile.writeline("objOpenFile.Close")
     objOpenFile.writeline("Set objFSO=Nothing")
     objOpenFile.writeline("Set objTempFolder=Nothing")
     objOpenFile.writeline("Set objSleepFile=Nothing")
     objOpenFile.writeline("Set objShell=Nothing")
     objOpenFile.writeline("End Sub")
     objOpenFile.writeline("</script>")
     objOpenFile.writeline("<hta:application applicationname=""Open File"" border=""dialog"" borderstyle=""normal"" caption=""Open File"" contextmenu=""no"" maximizebutton=""no"" minimizebutton=""no"" navigable=""no"" scroll=""no"" selection=""no"" showintaskbar=""no"" singleinstance=""yes"" sysmenu=""no"" version=""1.0"" windowstate=""minimize"">")
     objOpenFile.writeline("</head>")
     objOpenFile.writeline("<body>")
     objOpenFile.writeline("<input Application=""True"" type=""file"" id=""FileName"" />")
     objOpenFile.writeline("</body>")
     objOpenFile.writeline("</html>")
     objOpenFile.Close
     Set objShell = CreateObject("WScript.Shell")
     objShell.Run "mshta.exe " & strFullTempFileName,0,True
     objFSO.DeleteFile strFullTempFileName, True
     Set objShell=Nothing
     Set objOpenFile=Nothing
     strFullTempFileName = objTempFolder.Path & "\" & strTempTextFileName
     Set objTextFile=objFSO.OpenTextFile(strFullTempFileName, ForReading)
     ChooseFile = objTextFile.ReadLine 
     objTextFile.Close
     objFSO.DeleteFile strFullTempFileName, True
     Set objTextFile=Nothing
     Set objFSO=Nothing
     Set objTempFolder=Nothing
    End Function

    • Proposed as answer by MockMyBeret Monday, April 30, 2012 7:28 PM
    Wednesday, October 19, 2011 4:15 PM
  • This worked great for what I wanted.  How can this be modified to provide a Save As dialog?
    Friday, January 13, 2012 12:16 AM
  • This worked great for what I wanted.  How can this be modified to provide a Save As .

     

    Sorry this topic has beenclosed fo almost a year. PLease start a new topic.

     


    jv
    Friday, January 13, 2012 1:33 AM
  • This code will only work in Windows XP!

    See: http://www.robvanderwoude.com/vbstech_ui_fileopen.php for more info

    Wednesday, August 1, 2012 8:47 AM
  • 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>""")
    MsgBox oExec.StdOut.ReadAll
    • Proposed as answer by Rudi Degrande Wednesday, October 17, 2012 9:07 AM
    Sunday, August 12, 2012 5:44 AM
  • In HTA use

      Set oExec=CreateObject("WScript.Shell").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>""" )
      Tst = oExec.StdOut.ReadAll
      Tst = Replace( Tst, vbCRLF, "" )
      MsgBox "==" & Tst & "=="
    • Proposed as answer by Daz_1234 Tuesday, April 30, 2013 12:15 PM
    Friday, November 16, 2012 12:25 PM
  • dieseyer.de - great bit of code, cheers.

    To clarify, copy and paste dieseyer.de's code above into a VBScript in Windows 7 and it will show a browse dialog and correctly return the file path.  It even returns UNC paths correctly.

    Nice one, cheers!

    Daz.
    Oxford, UK.

    • Proposed as answer by Leandro_19 Tuesday, August 13, 2013 5:54 PM
    • Unproposed as answer by Leandro_19 Tuesday, August 13, 2013 5:54 PM
    Tuesday, April 30, 2013 12:19 PM
  • Open as IE

    Tools->Internet Options->security->click internet->custom level->miscellaneous(Enable) .

    Tools->Internet Options->security->click internet->custom level->Include local directory path when uploading file serve(Enable) .

    Good luck.

    Tuesday, August 13, 2013 5:59 PM
  • The result is the full pathname with a lineend char at the end. (The next post uses the replace function to correct this)

    If you use oExec.StdOut.ReadLine you got the path without newline

    Monday, October 14, 2013 1:22 PM
  • Works only with the 32bit wscript.exe

    You can force the script to run with the 32bit engine with:

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Set oShell = WScript.CreateObject ("WSCript.shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ElogName=replace(LCase(wScript.ScriptFullName),".vbs",".txt")
    Set Elog=objFSO.openTextFile(ElogName,8,True)
    Elog.Writeline vbCrLf & now() & vbTab & WScript.FullName & vbTab & wScript.ScriptFullName
    '''''''''''''' Force 32 bit Script Engine ''''''''''''''
    SysWoW64=replace(LCase(objFSO.GetSpecialFolder(1)) , "system32","syswow64")
    If objFSO.FolderExists(SysWoW64) then ' If Folder is there Are we using it?
    If ( InStr(LCase(WScript.FullName), "syswow64") = 0 ) Then
    Eng=replace(WScript.FullName,"System32","SysWOW64",1,1,1) 
    S=Wscript.ScriptFullName   
    Elog.WriteLine "Use: " & """" & Eng & """ """ & S & """"
    oshell.run """" & Eng & """ """ & S & """"
    WScript.Quit
    Else'''
    Elog.WriteLine "Using: " & """" & WScript.FullName 
    End If
     End If
     Elog.Close
     objFSO.DeleteFile( ElogName )


    Dim objDialog
    Set objDialog = CreateObject("MSComDlg.CommonDialog")   
    With objDialog
    .Filter = "*.txt"   
    .InitDir = "C:"   
    .MaxFileSize = 256   
    .Flags = &H80000 + &H4 + &H8 
    End With  
      
    If objDialog.ShowOpen = False Then
    WScript.Echo "You pressed 'Cancel' please try again."
      Wscript.Quit
    Else
    WScript.Echo objDialog.FileName
    End If

    '''''''''''''''''''''''''''''''''''''''''''

    Of course you need to install and register mscomdlg32.ocx in the SYSWOW64 folder

    Wednesday, October 7, 2015 4:37 PM