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:
  1. Option Explicit
  2.  
  3. Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _
  4.  (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
  5. Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" _
  6.  (ByVal pidl As Long, ByVal pszPath As String) As Long
  7.  
  8. Private Const ERROR_SUCCESS = 0
  9. Private Const MAX_LENGTH = 260
  10. 'Private Const CSIDL_COMMON_DOCUMENTS = &H2E
  11. Private Const CSIDL_PERSONAL = &H5
  12.  
  13. Public Function GetSpecialFolder() As String
  14. Dim sPath As String
  15. Dim pidl As Long
  16.  
  17.   'Get the ID list from the Path ID
  18.   If SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, pidl) = ERROR_SUCCESS Then
  19.     'Allocate the space for the path
  20.     sPath = Space$(MAX_LENGTH)
  21.     'Get the real path from the ID list
  22.     If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then
  23.       'Strip off the trailing null characters
  24.       GetSpecialFolder = Left$(sPath, InStr(sPath, Chr$(0)) - 1)
  25.       'And add a trailing \
  26.       If Right(GetSpecialFolder, 1) <> "\" Then GetSpecialFolder = GetSpecialFolder & "\"
  27.     End If
  28.   End If
  29. End Function