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
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
这篇博客介绍了如何使用VBS(Visual Basic Script)在Windows XP和2003系统中通过两种不同的方法浏览本地文件并获取文件的完整路径。在XP系统下,利用UserAccounts.CommonDialog对象实现;而在2003系统下,通过创建SAFRCFileDlg.FileOpen对象来实现文件选择。同时,还提及在Win7/IE8环境下可能存在的路径返回问题。
2643

被折叠的 条评论
为什么被折叠?



