Скопируйте и вставьте следующий код в стандартный модуль кода.Звоните следующим образом:
Debug.Print SpecFolder(CSIDL_PERSONAL)
'----- Special Folder declarations -------------------------------------------'
'"Identify the Location of Special Folders with API Calls" article in MSDN '
'See http://msdn.microsoft.com/en-us/library/aa140088(office.10).aspx for more info'
''
'Declaration section for APIs needed to get the path to My Documents:'
Private Declare Function SHGetSpecialFolderLocation _
Lib "Shell32" _
(ByVal hWnd As Long, ByVal nFolder As Long, ppidl As Long) As Long
Private Declare Function SHGetPathFromIDList _
Lib "Shell32" Alias "SHGetPathFromIDListA" _
(ByVal Pidl As Long, ByVal pszPath As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal pvoid As Long)
Private Const MAX_PATH = 260
Private Const NOERROR = 0
Public Enum CSIDL
CSIDL_PERSONAL = &H5 'Current user My Documents'
CSIDL_DESKTOPDIRECTORY = &H10 'Current user Desktop'
CSIDL_MYPICTURES = 39 'Current user My Pictures'
End Enum
'================================================================='
Function SpecFolder(ByVal Folder As CSIDL) As String
Dim PidlFound As Long, FolderFound As Long
Dim Pidl As Long, strPath As String
'Create buffer to hold path'
strPath = Space(MAX_PATH)
'Find Pointer to item ID List (PIDL)'
PidlFound = SHGetSpecialFolderLocation(0, Folder, Pidl)
If PidlFound = NOERROR Then
'Look up path to special folder using the PIDL we found above'
FolderFound = SHGetPathFromIDList(Pidl, strPath)
If FolderFound Then
'Return only the portion of the string buffer we want'
' (everything up to the null terminating character)'
SpecFolder = Left$(strPath, _
InStr(1, strPath, vbNullChar) - 1)
End If
End If
'When an API function creates a PIDL, memory is automatically allocated to storing it'
' CoTaskMemFree frees that allocated memory'
CoTaskMemFree Pidl
End Function