How to open windows folder without using username

2020-03-30 16:12发布

问题:

I am trying to open a windows folder (Downloads folder) via a commandbutton in a userform in Excel. However I want to use a generic folder path to access the downloads folder so whoever has this excel file can open their downloads folder. I got this code from the internet but it doesn't work, it only opens the document folder which I believe is the default folder.

Private Sub CommandButton1_Click()
   Shell "explorer.exe" & " " & "C:\Users\%USERNAME%\Downloads", vbNormalFocus
End Sub

How can I achieve that?

回答1:

You want to use the Environ() function:

"C:\Users\" & Environ("Username") & "\Downloads"

Environ Function