vbs 获取跨服务器文件名,使用VBS浏览本地文件的3种方式,获取完整路径

本文分享了在XP系统和IE8浏览器中遇到的问题及解决方案,对比了两种文件选择方法:传统对话框与IE浏览器操作。重点介绍了修改后的可用代码ChooseFile,适合跨平台和兼容性需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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 "

"

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 "

"

With .All.f

.Focus

.Click

Result = .Value

End With

End With

.Quit

End With

Set IE = Nothing

ChooseFile = Result

End Function

ChooseFile

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

时间: 2019-12-08

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值