文章摘要
这篇文章介绍了一个在IE浏览器中通过VBA自定义函数实现文件选择的方法。函数`BrowseForFile`通过创建InternetExplorer对象并配置其显示属性,然后在页面上输入表单输入项来捕获用户选择的文件路径。该方法利用了Win32 API和VBA编程,无需打开IE浏览器即可完成文件路径的捕获。需要注意的是,该方法仅适用于IE版本,且在非IE浏览器环境下可能失效。
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
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
© 版权声明
文章版权归作者所有,未经允许请勿转载。



