The supported way to read such paths is to use the SHGetKnownFolderPath
function.
I wrote this VBA code to do that. It has been tested in Excel 2000.
It won't work in any 64-bit version of Office. I don't know if its Unicode shenanigans will work in versions of Office more recent than 2000. It's not pretty.
Option Explicit
Private Type GuidType
data1 As Long
data2 As Long
data3 As Long
data4 As Long
End Type
Declare Function SHGetKnownFolderPath Lib "shell32.dll" (ByRef guid As GuidType, ByVal flags As Long, ByVal token As Long, ByRef hPath As Long) As Long
Declare Function lstrlenW Lib "kernel32.dll" (ByVal hString As Long) As Long
Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMemory As Long)
Declare Sub RtlMoveMemory Lib "ntdll.dll" (ByVal dest As String, ByVal source As Long, ByVal count As Long)
'Read the location of the user's "Downloads" folder
Function DownloadsFolder() As String
' {374DE290-123F-4565-9164-39C4925E467B}
Dim FOLDERID_Downloads As GuidType
FOLDERID_Downloads.data1 = &H374DE290
FOLDERID_Downloads.data2 = &H4565123F
FOLDERID_Downloads.data3 = &HC4396491
FOLDERID_Downloads.data4 = &H7B465E92
Dim result As Long
Dim hPath As Long
Dim converted As String
Dim length As Long
'A buffer for the string
converted = String$(260, "*")
'Convert it to UNICODE
converted = StrConv(converted, vbUnicode)
'Get the path
result = SHGetKnownFolderPath(FOLDERID_Downloads, 0, 0, hPath)
If result = 0 Then
'Get its length
length = lstrlenW(hPath)
'Copy the allocated string over the VB string
RtlMoveMemory converted, hPath, (length + 1) * 2
'Truncate it
converted = Mid$(converted, 1, length * 2)
'Convert it to ANSI
converted = StrConv(converted, vbFromUnicode)
'Free the memory
CoTaskMemFree hPath
'Return the value
DownloadsFolder = converted
Else
Error 1
End If
End Function
"C:\Users\" & Environ("UserName") & "\Downloads"
? – Ergotismc:\Users\simoco
, the Documents folder can easily be moved tod:\stuff
. – AttributiveEnviron("USERPROFILE") & "\Downloads"
- but that doesn't answer the question in the sense that a user may rename his Downloads folder. – Nonu