当前位置: 代码网 > it编程>前端脚本>其它脚本 > 使用VBS浏览本地文件的3种方式,获取完整路径

使用VBS浏览本地文件的3种方式,获取完整路径

2024年05月15日 其它脚本 我要评论
因为系统组件差异,所以代码也有不同文章最后代码网给出一个通用的代码xp下:function browseforfile( )' select file dialog based on a script

因为系统组件差异,所以代码也有不同文章最后代码网给出一个通用的代码

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选择本地文件功能的代码就介绍到这了,需要的朋友可以参考一下。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com