使用VBS浏览本地文件的3种方式,获取完整路径
因为系统组件差异,所以代码也有不同文章最后毛票票给出一个通用的代码
XP下:
FunctionBrowseForFile()
'SelectFiledialogbasedonascriptbyMayayana
'Knownissues:
'*TreeviewalwaysopensDesktopfolder
'*InWin7/IE8onlythefileNAMEisreturnedcorrectly,thepathreturnedwillalwaysbeC:\fakepath\
'*Ifashortcuttoafileisselected,thenameofthatFILEwillbereturned,nottheshortcut's
'OnErrorResumeNext
'===========1,filebrowsererinXP==================
SetobjDialog=CreateObject("UserAccounts.CommonDialog")
objDialog.Filter="txt|*.txt"
objDialog.InitialDir="C:\"
intResult=objDialog.ShowOpen
IfintResult<>0Then
BrowseForFile=objDialog.FileName
exitfunction
else
msgbox"Error."
EndIf
endfunction
BrowseForFile
2003下'forwindows2003
FunctionSelectAFile
SetobjDialog=CreateObject("SAFRCFileDlg.FileOpen")
intResult=objDialog.OpenFileOpenDlg
SelectAFile=objDialog.FileName
EndFunction
浏览器方式:
FunctionBrowseForFile()
'===========2,fileselectinIE======================
'Anotherwaytogetfilepath
DimobjIE,strSelected
BrowseForFile=""
SetobjIE=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")
DoUntilobjIE.ReadyState=4
Loop
'Centerthedialogwindowonthescreen
WithobjIE.Document.ParentWindow.Screen
objIE.Left=(.AvailWidth-objIE.Width)\4
objIE.Top=(.Availheight-objIE.Height)\4
EndWith
objIE.Document.Write""
WithobjIE.Document.all.FileSelect
.focus
.click
strSelected=.value
EndWith
objIE.Quit
SetobjIE=Nothing
IfTrim(strSelected)=""Then
Msgbox"Youselectednofile."
Wscript.Quit
EndIf
BrowseForFile=strSelected
EndFunction
BrowseForFile
有没有发现上面的代码运行不易啊,这里毛票票小编为大家分享一个可用的,虽然上面的的代码不能用但学习参考一下还是不错的,现在因为平台愿意导致,原来这些代码都是可以使用的。
FunctionChooseFile()
DimResult
Result=""
DimIE:SetIE=CreateObject("InternetExplorer.Application")
WithIE
.Visible=False
.Navigate("about:blank")
DoUntil.ReadyState=4:Loop
With.Document
.Write""
With.All.f
.Focus
.Click
Result=.Value
EndWith
EndWith
.Quit
EndWith
SetIE=Nothing
ChooseFile=Result
EndFunction
ChooseFile
好了这篇关于vbs选择本地文件功能的代码就介绍到这了,需要的朋友可以参考一下。