Option Explicit
Private Const CSIDL_PERSONAL = &H5
Private Declare Function SHGetSpecialFolderLocation _
Lib "shell32.dll" _
(ByVal hwndOwner As Long, _
ByVal nFolder As Long, _
pidl As ITEMIDLIST) _
As Long
Private Declare Function SHGetPathFromIDList _
Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) _
As Long
Private Declare Sub CoTaskMemFree _
Lib "ole32.dll" _
(ByVal pv As Long)
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Sub Form_Click()
MsgBox GetMyDocumentsLocation(hWnd)
End Sub
Private Function GetMyDocumentsLocation(hWnd As Long) As String
Dim lResult As Long
Dim sPath As String
Dim idlist As ITEMIDLIST
On Error GoTo ErrHandler
' populate an ITEMIDLIST struct with the specified folder information
lResult = SHGetSpecialFolderLocation(hWnd, CSIDL_PERSONAL, idlist)
If lResult = 0 Then
' if the information is present, get the path information
sPath = Space$(260)
lResult = SHGetPathFromIDList(ByVal idlist.mkid.cb, _
ByVal sPath)
' free memory allocated by shell
CoTaskMemFree idlist.mkid.cb
' if a path was found, trim off trailing null char
sPath = Left$(sPath, InStr(1, sPath, vbNullChar) - 1)
GetMyDocumentsLocation = sPath
End If
ErrExit:
Exit Function
ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation,
"GetMyDocumentsLocation"
Resume ErrExit
End Function
HTH
Earl Damron, MCSD, MVP (Visual Basic)
earld...@mail.com
Mehdy Ghanaee wrote in message ...