Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Relative path from FileDialog Box

Status
Not open for further replies.

BigKeeya

Programmer
Nov 8, 2003
6
0
0
US
When I use a FileDialog Box to return the name of a file, it returns the absolute file name (e.g., C:/documents and setting/usernmae/desktop/filename.xls).


Is there a way to retunr an relative file name, so that my VBA program can find the correct file on any computer, not just mine.
 
Have a look at the Mid and InStrRev functions.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Here is a function from John Walkenbach, on his "The Spreadsheet Page" website:
Code:
Function FileNameOnly(ByVal PathName As String) As String
' Returns a filename from a path/filename string
Dim i As Integer
Dim Length As Integer
Dim Temp As String

   Length = Len(PathName)
   Temp = ""
   For i = Length To 1 Step -1
     If Mid$(PathName, i, 1) = Application.PathSeparator Then
       FileNameOnly = Temp
       Exit Function
     End If
     Temp = Mid$(PathName, i, 1) & Temp
   Next i
   FileNameOnly = PathName
   
End Function

Regards,
Mike
 
Mike, here an oneliner version of your post:
FileNameOnly = Mid(PathName, 1 + InStrRev(PathName, Application.PathSeparator))

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks, PH

I suspect John wrote that prior to XL2000. Yours is much more compact.

Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top