Reputation: 38213
I have this function that will handle relative paths by converting them to absolute.
Public Function ifRelativeConvertToAbsolutePath(path As String) As String
If (isPathRelative(path)) Then
ifRelativeConvertToAbsolutePath = convertToAbsolutePath(path)
Else
ifRelativeConvertToAbsolutePath = path
End If
End Function
How could I implement the isPathRelative
function?
My ideas include:
Upvotes: 0
Views: 3871
Reputation: 27220
What if you did the following:
If not Dir( thisWorkbook.Path & "\" & path, vbDirectory) = vbNullString then
ifRelativeConvertToAbsolutePath = thisWorkbook.Path & "\" & path
else
ifRelativeConvertToAbsolutePath = path
end if
Basically, try concatenating the paths. If it results in a valid path, then it was a relative path and you just fixed it, if it does not result in a valid path, then you cannot use that method to fix the path, so just return the path as it was.
Upvotes: 2
Reputation: 175866
There is also a shell API for that (PathIsRelative
), or there is PathCombine
which will automatically convert a rel path to an abs path if it receives one;
Private Declare Function PathCombine Lib "shlwapi.dll" Alias "PathCombineA" (ByVal szDest As String, ByVal lpszDir As String, ByVal lpszFile As String) As Long
Dim sBuff As String * 255
PathCombine sBuff, "C:\theroot\xxx\", "..\jibblets"
x = Left$(sBuff, InStr(1, sBuff, vbNullChar) - 1)
== "C:\theroot\jibblets"
PathCombine sBuff, "C:\theroot\xxx\", "X:\foo.bar"
== "X:\foo.bar"
Upvotes: 3
Reputation: 25262
Check if it starts with "\" (that includes "\" for UNC) or with 1 letter + ":\"
Function IsPathRel(p As String) As Boolean
IsPathRel = Not ((Left(p, 3) Like "[A-Z]:\") Or (Left(p, 1) = "\"))
End Function
Upvotes: 2