您好,登錄后才能下訂單哦!
怎么在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
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。