Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find My Documents path ?

184 views
Skip to first unread message

Mehdy Ghanaee

unread,
Jan 28, 2001, 3:13:27 AM1/28/01
to
Hi,
How can I find "My Documents" path by VB ?

Earl Damron

unread,
Jan 28, 2001, 8:37:15 AM1/28/01
to
Paste this code into a form, run it, and click on the form. The key is the
SHGetSpecialFolderLocation(hWnd, CSIDL_PERSONAL, idlist) line and the
CSIDL_PERSONAL constant. Check out MSDN to find other constants you can use
for other "special" folders.


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 ...

0 new messages