Get the Windows Download folder's path

后端 未结 4 1173
失恋的感觉
失恋的感觉 2021-01-05 05:24

I have some Excel VBA code that requires knowing the Downloads folder path. How could I do it?

Since you can move around the Downloads folder (and

4条回答
  •  难免孤独
    2021-01-05 06:07

    Simple Solution - usually works

    This is from a comment by @assylias. As others have mentioned it will provide the wrong folder path if the user has changed the default "Downloads" location - but it's simple.

    Function GetDownloadsPath() As String
        GetDownloadsPath = Environ$("USERPROFILE") & "\Downloads"
    End Function
    

    Best Solution

    The posted answer was returning "%USERPROFILE%\Downloads". I didn't know what to do with it so I created the function below. This turns it into a function and returns the actual path. Call it like this: Debug.Print GetCurrentUserDownloadsPath or Debug.Print GetCurrentUserDownloadsPath. Thanks to @s_a for showing how to read a registry key and finding the registry key with the folder path.

    ' Downloads Folder Registry Key
    Private Const GUID_WIN_DOWNLOADS_FOLDER As String = "{374DE290-123F-4565-9164-39C4925E467B}"
    Private Const KEY_PATH As String = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\"
    '
    Public Function GetCurrentUserDownloadsPath()
        Dim pathTmp As String
    
        On Error Resume Next
        pathTmp = RegKeyRead(KEY_PATH & GUID_WIN_DOWNLOADS_FOLDER)
        pathTmp = Replace$(pathTmp, "%USERPROFILE%", Environ$("USERPROFILE"))
        On Error GoTo 0
    
        GetCurrentUserDownloadsPath = pathTmp
    End Function
    '
    Private Function RegKeyRead(registryKey As String) As String
    ' Returns the value of a windows registry key.
        Dim winScriptShell As Object
    
        On Error Resume Next
        Set winScriptShell = VBA.CreateObject("WScript.Shell")  ' access Windows scripting
        RegKeyRead = winScriptShell.RegRead(registryKey)    ' read key from registry
    End Function
    

提交回复
热议问题