温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在VBS中浏览本地文件

发布时间:2021-03-23 14:58:55 来源:亿速云 阅读:181 作者:Leah 栏目:开发技术

怎么在VBS中浏览本地文件?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vbs
AI