在Win7系统上通过VBScript实现文件浏览选择功能

实现函数代码:

Function BrowseForFile()
    Set fso=CreateObject("Scripting.FileSystemObject")
    Set tempFile=fso.CreateTextFile("tmpfile.hta")
    tempFile.Write _
    "<html>" & Chr(13) & Chr(10) & _
    "<head>" & Chr(13) & Chr(10) & _
    "<title>Browse</title>" & Chr(13) & Chr(10) & _
    "</head>" & Chr(13) & Chr(10) & _
    "<body>" & Chr(13) & Chr(10) & _
    "<input type='file' id='file'/>" & Chr(13) & Chr(10) & _
    "<script type='text/javascript'>" & Chr(13) & Chr(10) & _
    "var ForReading=1;" & Chr(13) & Chr(10) & _
    "var ForWriting=2;" & Chr(13) & Chr(10) & _
    "var file = document.getElementById('file');" & Chr(13) & Chr(10) & _
    "file.click();" & Chr(13) & Chr(10) & _
    "var fso=new ActiveXObject('Scripting.FileSystemObject');" & Chr(13) & Chr(10) & _
    "fpw=fso.CreateTextFile('tmpfilepath');" & Chr(13) & Chr(10) & _
    "fpw.Write(file.value);" & Chr(13) & Chr(10) & _
    "fpw.close();" & Chr(13) & Chr(10) & _
    "window.close();" & Chr(13) & Chr(10) & _
    "</script>" & Chr(13) & Chr(10) & _
    "</body>" & Chr(13) & Chr(10) & _
    "</html>"
    tempFile.Close
    Set shell=CreateObject("WScript.Shell")
    shell.Run "tmpfile.hta",0,True    
    Set tempFile=fso.GetFile("tmpfilepath")    
    If tempFile.size>0 Then    
        Set tempFile=fso.OpenTextFile("tmpfilepath",1,false) 
        BrowseForFile=tempFile.ReadAll     
        tempFile.close
    Else
        BrowseForFile=""
    End If    
    Set tempFile=fso.GetFile("tmpfile.hta")
    tempFile.attributes=0
    tempFile.delete
    Set tempFile=fso.GetFile("tmpfilepath")
    tempFile.attributes=0
    tempFile.delete    
    Set shell=Nothing
    Set fso=Nothing
End Function

具体调用方法:

Dim selpath
selpath=BrowseForFile()
If selpath <> "" Then
    MsgBox("你已经选择的文件"&selpath)
Else
    MsgBox("你还没有选择文件")
End If

猜你喜欢

转载自blog.csdn.net/princewwj/article/details/82905682
今日推荐