Instantly share code, notes, and snippets.

Embed
What would you like to do?
VBA functions that wrap GetShortPathName/GetLongPathName of Win32API.
#If VBA7 Then
Private Declare PtrSafe Function Win32APIGetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As LongLong) As LongLong
#Else
Private Declare Function Win32APIGetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As LongLong) As Long
#End If
#If VBA7 Then
Private Declare PtrSafe Function Win32APIGetLongPathName Lib "kernel32" Alias "GetLongPathNameA" _
(ByVal lpszShortPath As String, ByVal lpszLongPath As String, ByVal cchBuffer As LongLong) As LongLong
#Else
Private Declare Function Win32APIGetLongPathName Lib "kernel32" Alias "GetLongPathNameA" _
(ByVal lpszShortPath As String, ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long
#End If
Function GetShortPathName(ByVal longPath As String) As String
Const PATH_LENGTH = 260
Dim pathBuffer As String
pathBuffer = String$(PATH_LENGTH + 1, vbNull)
#If VBA7 Then
Dim pathLength As LongLong
#Else
Dim pathLength As Long
#End If
pathLength = Win32APIGetShortPathName(longPath, pathBuffer, PATH_LENGTH)
GetShortPathName = Left(pathBuffer, CLng(pathLength))
End Function
Function GetLongPathName(ByVal shortPath As String) As String
Const PATH_LENGTH = 260
Dim pathBuffer As String
pathBuffer = String$(PATH_LENGTH + 1, vbNull)
#If VBA7 Then
Dim pathLength As LongLong
#Else
Dim pathLength As Long
#End If
pathLength = Win32APIGetLongPathName(shortPath, pathBuffer, PATH_LENGTH)
GetLongPathName = Left(pathBuffer, CLng(pathLength))
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment