在VBA中实现LISP中findfile()函数功能
函数内容:Function FindFile(strFile As String)
Dim PrefFile As AcadPreferencesFiles
Dim curSupportPath As String
Dim numPath As Integer
Dim strPath As String
Dim pathName As String
Dim PathTF As Boolean
Dim SearchPath As String
Dim appPath As string
Set PrefFile = ThisDrawing.Application.Preferences.Files
curSupportPath = PrefFile.SupportPath
PathTF = True
pathName = CurDir() & "\" & strFile
If Dir(pathName) = "" Then
appPath = ThisDrawing.Application.Path
PathName = appPath & "\" & strFile
If Dir(pathName) = "" Then
Do While PathTF
numPath = InStr(1, curSupportPath, ";", 1)
If numPath = 0 Then
strPath = curSupportPath
PathTF = False
Else
strPath = Left(curSupportPath, numPath - 1)
curSupportPath = Right(curSupportPath, Len(curSupportPath) - numPath)
End If
pathName = strPath & "\" & strFile
If Dir(pathName) <> "" Then
SearchPath = pathName
Exit Do
End If
Loop
pathName = strPath & "\" & strFile
If Dir(pathName) = "" Then
SearchPath = ""
End If
Else
SearchPath = pathName
End If
Else
SearchPath = pathName
End If
FindFile = SearchPath
End Function
参数:
一个文件名
注意:
在LISP中, findfile()函数的功能是在autoCAD的当前目录、autoCAD目录及autoCAD的支持目录下查找所需的文件,如果找到文件,则返回文件的路径及文件名称,如不能找到文件,则返回空字符。
在VBA中,没有相应的函数来完成该功能,而必须使用绝对路径来引用文件,这就为VBA在autoCAD中的使用带来麻烦,有时你编制了一个VBA程序,并提供给用户,由于用户安装的路径和你程序调试的路径不同,致使程序运行出错。
这种情况会经常发生的,因为你无权要求用户必须将程序放在哪个固定的目录下。
示例:
strPathFile = FindFile("tyl.ini")
页:
[1]