在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

princewwj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值