Created
January 27, 2015 12:58
-
-
Save knjname/ba3b7d433655d5930a08 to your computer and use it in GitHub Desktop.
VBA functions that wrap GetShortPathName/GetLongPathName of Win32API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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