Skip to content

Instantly share code, notes, and snippets.

@knjname
Created January 27, 2015 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knjname/ba3b7d433655d5930a08 to your computer and use it in GitHub Desktop.
Save knjname/ba3b7d433655d5930a08 to your computer and use it in GitHub Desktop.
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