在指定的分区中搜索某个文件并获得文件路径的函数
第一步:将以下代码复制到模块中:
'***************** 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"或以上的版本
(责任编辑:admin)
- ·关于 Partition 函数在分组查询中的应
- ·Access算术运算符的含义和说明表
- ·mid函数的另类用法
- ·access制作程序运行进度框
- ·Function与Sub的异同(函数调用)
- ·Access判断某个数值是否为某个数据类型
- ·select case后面语句块的值的四种格式
- ·vba条件语句的两种表示方法
- ·Access几种数据类型初始化的值
- ·Access vba null与""空字符串的区别
- ·access vba 数据类型表
- ·Access变量的命名规则
- ·Access中EXIT Sub与End Sub的区别
- ·Access vba中参数前关键字ByRef和ByVal
- ·Access列表框快速全选的技巧【最快】
- ·vba函数的数据类型强制转换