因为系统组件差异,所以代码也有不同文章最后代码网给出一个通用的代码
xp下:
function browseforfile( )
' select file dialog based on a script by mayayana
' known issues:
' * tree view always opens desktop folder
' * in win7/ie8 only the file name is returned correctly, the path returned will always be c:\fakepath\
' * if a shortcut to a file is selected, the name of that file will be returned, not the shortcut's
'on error resume next
'===========1, file browserer in xp ==================
set objdialog = createobject("useraccounts.commondialog")
objdialog.filter = "txt|*.txt"
objdialog.initialdir = "c:\"
intresult = objdialog.showopen
if intresult <> 0 then
browseforfile = objdialog.filename
exit function
else
msgbox "error."
end if
end function
browseforfile
2003下'for windows 2003
function selectafile
set objdialog = createobject("safrcfiledlg.fileopen")
intresult = objdialog.openfileopendlg
selectafile = objdialog.filename
end function
浏览器方式:
function browseforfile( )
'===========2, fileselect in ie ======================
'another way to get file path
dim objie, strselected
browseforfile = ""
set objie = createobject( "internetexplorer.application" )
objie.toolbar = false
objie.resizable = false
objie.statusbar = false
objie.width = 300
objie.height = 100
objie.visible = true
objie.navigate( "about:blank" )
do until objie.readystate = 4
loop
' center the dialog window on the screen
with objie.document.parentwindow.screen
objie.left = (.availwidth - objie.width ) \ 4
objie.top = (.availheight - objie.height) \ 4
end with
objie.document.write "<html><body><input id=""fileselect"" name=""fileselect"" type=""file""><body></html>"
with objie.document.all.fileselect
.focus
.click
strselected = .value
end with
objie.quit
set objie = nothing
if trim(strselected) = "" then
msgbox "you selected no file."
wscript.quit
end if
browseforfile = strselected
end function
browseforfile
有没有发现上面的代码运行不易啊,这里代码网小编为大家分享一个可用的,虽然上面的的代码不能用但学习参考一下还是不错的,现在因为平台愿意导致,原来这些代码都是可以使用的。
function choosefile()
dim result
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
choosefile
好了这篇 关于vbs选择本地文件功能的代码就介绍到这了,需要的朋友可以参考一下。
发表评论