"Mark" wrote ...
> anybody has some sample code on the equivalent of getfiled in VBA?
Woooo - I had to dig 'way back in my archives for this one. I wrote this back
in early 2000 I think, it was picked up by Autodesk to re-print in their VBA
Technical Newsletter. That's the version listed below, so it's been reformatted a few
times and the comments tend to run into code awkwardly, but hopefully it will help.
I've been out of VBA for a while now, so be careful of the section 'Check in
Drawing's Directory' - the ThisDrawing.Name and ThisDrawing.FullName properties may
return different strings that they used to in the version of ACAD I used back then.
- - - - - copy of code below - - - - -
The code shown here has been modified in format only
to fit within the confines of the newsletter format.
'----FindFile----------------------------------------
' This function looks for the specified filename in
' the current directory, the current drawing's
' directory, any of AutoCAD's support paths, and the
' directory ACAD is running from. If it finds
' the file, it will return the full path+filename.
' If not, it will return an empty string. If the
' specified filename includes any backslashes ( \ ),
' it is assumed that it's a full path+filename
' which must be checked to see if it exists, rather
' than searching amongst AutoCAD's paths. This
' function does not really work with wildcards. If it
' finds a file which matches the filespec, it
' will return the path plus the original filespec,
' eg. C:\*.bat, rather than C:\Autoexec.bat .
'-----------------------------------------------------
Public Function FindFile(FileName As String) As String
Dim Path As String 'holds path to check
Dim SupportPath As String 'copy of ACAD's support paths
Dim Pos As Integer 'position within SupportPath of separator
'----if supplied name has embedded '\' assume it is full path+filename
If InStr(1, FileName, "\") > 0 Then 'look for embedded backslash
FindFile = FileName 'copy supplied path+filename for result
On Error Resume Next 'in case of disk error
'if doesn't exist, change result to ""
If Dir$(FileName) = "" Then FindFile = ""
If Err Then FindFile = "" 'if some file error, indicate failure
Exit Function 'return result to calling routine
End If
'----check in current directory---------------------
'construct path+filename to check
FindFile = CurDir$ & "\" & FileName
'if exists, return it as the result
If Dir$(FindFile) <> "" Then Exit Function
'----check in drawing's directory-------------------
'in ACAD 14.01, same as ThisDrawing.Path
Path = ThisDrawing.FullName
'ensure not a new, unnamed drawing
If Path <> "" Then
'chop drawing name from end of 'path'
Path = Left$(Path, Len(Path) - Len(ThisDrawing.Name))
'construct path+filename
FindFile = Path & FileName
'if exists, return it as the result
If Dir$(FindFile) <> "" Then Exit Function
End If
'----check ACAD's support directories----------------
'copy of support paths
SupportPath = ThisDrawing.Application.Preferences.SupportPath
'ensure has trailing '\'
If Right$(SupportPath, 1) <> "\" Then SupportPath = SupportPath & "\"
'check each one until none left
Do While Len(SupportPath) > 0
'look for separator between paths
Pos = InStr(1, SupportPath, ";")
'if found a separator character...
If Pos > 0 Then
'...extract path before it
Path = Left$(SupportPath, Pos - 1)
'no separator, so...
Else
'...must be last path in SupportPath
Path = SupportPath
End If
'make sure has trailing backslash
If Right$(Path, 1) <> "\" Then Path = Path & "\"
'construct path+filename to check
FindFile = Path & FileName
'if exists, return as result
If Dir$(FindFile) <> "" Then Exit Function
'chop off path just checked
SupportPath = Right$(SupportPath, Len(SupportPath) - Len(Path))
'probably separator still there
If Left$(SupportPath, 1) = ";" Then
'chop it off too
SupportPath = Right$(SupportPath, Len(SupportPath) - 1)
End If
'check next path in SupportPath
Loop
'----check in AutoCAD's program directory-------------
'get path to ACAD.EXE
Path = ThisDrawing.Application.Path
'make sure has trailing backslash
If Right$(Path, 1) <> "\" Then Path = Path & "\"
'construct path+filename to check
FindFile = Path & FileName
'if exists, return as result
If Dir$(FindFile) <> "" Then Exit Function
'----can't find file, return empty string to indicate failure
FindFile = ""
End Function
'----TestFindFile----------------------------------------
' This routine is used to test the FindFile function.
' It brings up an InputBox so the user can
' enter the name of a file to check for, calls FindFile(),
' then displays the result in a MsgBox.
'--------------------------------------------------------
Public Sub TestFindFile()
Dim res As String 'holds the result of function calls
Dim msg As String messages to present to user
'----prompt user for filename to test
msg = "Enter the filename you want to try with the FindFile function." &
vbCrLf & vbCrLf
msg = msg & "FindFile will look in the current directory, the directory" &
vbCrLf
msg = msg & "containing the current drawing (if any), AutoCAD's support" &
vbCrLf
msg = msg & "directories, and the directory AutoCAD is running from." &
vbCrLf
res = InputBox(msg, "Testing FindFile", "acad14.cfg")
'----use the FindFile function retrieve full path+filename
'if user pressed 'Cancel', res = ""
If res <> "" Then
'search for file
res = FindFile(res)
'FindFile returns "" if can't find file
If res = "" Then
'compose message to that effect and
msg = "Couldn't find that file."
'tell user what happened
MsgBox msg, vbInformation, "Testing FindFile"
Else
'FindFile returned full path+filename
msg = "Found that file in: '" & res & "'."
'show result to user
MsgBox msg, vbInformation, "Testing FindFile"
End If
End If
End Sub