在指定的分区中搜索某个文件并获得文件路径的函数
时 间:2009-02-06 14:30:24
作 者:竹笛 ID:8 城市:上海 QQ:2851379730
摘 要:有时,我们知道一个C盘的一个文件(例如:1.gif),当我们需要查找出这个文件的具体路径时,我们可以采用下面的代码来实现。
正 文:
第一步:将以下代码复制到模块中:
'***************** Code Start ***************
'This code was originally written by Dev Ashish.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Function fReturnFilePath(strFilename As String, _
strDrive As String) As String
Dim varItm As Variant
Dim strFiles As String
Dim strTmp As String
If InStr(strFilename, ".") = 0 Then
MsgBox "Sorry!! Need the complete name", vbCritical
Exit Function
End If
strFiles = ""
With Application.FileSearch
.NewSearch
.LookIn = strDrive
.SearchSubFolders = True
.FileName = strFilename
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute > 0 Then
For Each varItm In .FoundFiles
strTmp = fGetFileName(varItm)
If strFilename = strTmp Then
fReturnFilePath = varItm
Exit Function
End If
Next varItm
End If
End With
End Function
Private Function fGetFileName(strFullPath) As String
Dim intPos As Integer, intLen As Integer
intLen = Len(strFullPath)
If intLen Then
For intPos = intLen To 1 Step -1
'Find the last \
If Mid$(strFullPath, intPos, 1) = "\" Then
fGetFileName = Mid$(strFullPath, intPos + 1)
Exit Function
End If
Next intPos
End If
End Function
第二步:在窗体中建一个命令按钮Command1,在按钮的单击事件中写代码:
Private Sub Command1_Click()
strFilePath = fReturnFilePath("1.gif", "D:")
MsgBox "文件路径为:" & strFilePath
End Sub
第三步:在VBA的引用中,引用一下:Microsoft Office 8.0 Object Library"或以上的版本
Access软件网QQ交流群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access选项卡示例】Ac...(09.09)
- 【Access源码示例】按输入...(09.02)
- 【Access日期区间段查询】...(08.29)
- 【Access日期区间段查询】...(08.27)
- Access怎样才能实现日期时...(08.21)
- 【Access定时打开查询】A...(08.19)
- Access生成固定数量的记录...(08.13)
- Access怎样才能实现日期时...(08.12)
- Access利用导航窗体控件对...(08.03)
学习心得
最新文章
- Access表中的字段名、字段标题...(09.19)
- Access快速开发平台--更改“...(09.18)
- 【中秋及国庆优惠】Access培训...(09.15)
- Access如何将日期型的数值转换...(09.14)
- 英文输入法输入数据中存在单引号引起...(09.11)
- 【Access选项卡示例】Acce...(09.09)
- 让Access光标停留在指定的控件...(09.07)
- 关于Access查询条件里使用通配...(09.06)
- Access报表偷懒制作法--Ac...(09.05)
- Access快速开发平台--窗体数...(09.04)