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

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

时间2024-02-11 18:45:03发布访客分类脚本浏览903
导读:收集整理的这篇文章主要介绍了使用VBS浏览本地文件的3种方式,获取完整路径,觉得挺不错的,现在分享给大家,也给大家做个参考。 因为系统组件差异,所以代码也有不同文章最后给出一个通用的代码...
收集整理的这篇文章主要介绍了使用VBS浏览本地文件的3种方式,获取完整路径,觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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.ShowOpenIf intResult >
     0 Then	BrowseForFile = objDialog.FileName	exit functionelse	msgbox "Error."End Ifend functionBrowseForFile

2003下'for windows 2003

Function SelectAFileSet objDialog = CreateObject("SAFRCFileDlg.FileOpen")intResult = objDialog.OpenFileOpenDlgSelectAFile = objDialog.FileNameEnd Function

浏览器方式:

Function BrowseForFile( ) '===========2, fileselect in IE ======================  'Another way to get file path  Dim objIE, strSelected  BrowseForFile = ""  Set objIE = CreateObject( "internetExplorer.Application" )objIE.ToolBar = FalseobjIE.Resizable = FalseobjIE.statusbar = False objIE.Width = 300objIE.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 = strSelectedEnd FunctionBrowseForFile

有没有发现上面的代码运行不易啊,这里小编为大家分享一个可用的,虽然上面的的代码不能用但学习参考一下还是不错的,现在因为平台愿意导致,原来这些代码都是可以使用的。

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 = ResultEnd FunctionChooseFile

好了这篇 关于vbs选择本地文件功能的代码就介绍到这了,需要的朋友可以参考一下。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 使用VBS浏览本地文件的3种方式,获取完整路径
本文地址: https://pptw.com/jishu/610176.html
VBS遍历Excel工作表的实现代码 VBS进程判断代码

游客 回复需填写必要信息