I am trying to use an api to get the path to the my documuments folder. On my machine it works very well. However, when I test it on a machine that has the My Documents folder on a different drive, it fails.
Here is the code. I got it off line, possibly here, or another site. I looked at a bunch of places.
Anyone know how I can compensate for if the My Documents folder is on a different drive? I have looked around on google and here and haven't come accross anything that addresses that situation.
Thank you for your help.
VB Code:
Option Explicit Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _ (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" _ (ByVal pidl As Long, ByVal pszPath As String) As Long Private Const ERROR_SUCCESS = 0 Private Const MAX_LENGTH = 260 'Private Const CSIDL_COMMON_DOCUMENTS = &H2E Private Const CSIDL_PERSONAL = &H5 Public Function GetSpecialFolder() As String Dim sPath As String Dim pidl As Long 'Get the ID list from the Path ID If SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, pidl) = ERROR_SUCCESS Then 'Allocate the space for the path sPath = Space$(MAX_LENGTH) 'Get the real path from the ID list If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then 'Strip off the trailing null characters GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1) 'And add a trailing \ If Right(GetSpecialFolder, 1) <> "\" Then GetSpecialFolder = GetSpecialFolder & "\" End If End If End Function




Reply With Quote