2

I'm trying to obtain the desktop path in a situation where OneDrive may be a factor. Other simpler methods like Environ$("UserProfile") & "/Desktop" do not work. Here's what I've tried in a module:

Option Compare Database
Option Explicit


Private Declare PtrSafe Function SHGetKnownFolderPath Lib "shell32.dll" _
    (ByRef rfid As GUID, dwFlags As Long, hToken As Long, ByRef pszPath As Long) As Long

Private Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long


Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

Private Const FOLDERID_Desktop As String = "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}"

Public Function GetDesktopPath() As String
    Dim pszPath As Long
    Dim rfid As GUID
    Dim ret As Long

    rfid.Data1 = CLng(FOLDERID_Desktop)
    ret = SHGetKnownFolderPath(rfid, 0, 0, pszPath)

    If ret = 0 Then
        GetDesktopPath = StrConv(StrPtr(GlobalLock(pszPath)), vbUnicode)
        GlobalUnlock pszPath
    Else
        GetDesktopPath = ""
    End If
End Function

When I run this with debug.print GetDesktopPath(), I get a "Mismatch" error.

plateriot
  • 361
  • 5
  • 23

1 Answers1

9

Getting Paths of Special Folders on Windows in VBA

1. Using the WshShell Object

you can get a folder path by its SpecialFolderName.

The easiest way of obtaining the Desktop path is by using the .SpecialFolders property of the WshShell Object.

Public Function GetDesktopPath() As String
    GetDesktopPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
End Function

For the sake of completeness, I will also mention here how to use early binding with this Object.
To do this, you have to add a reference to the Windows Script Host Object Model.
You can then use the Object like this:

Public Function GetDesktopPath() As String
    'Requires reference to "Windows Script Host Object Model"
    Dim oWshShell As New WshShell
    GetDesktopPath = oWshShell.SpecialFolders("Desktop")
End Function

The other special folders accessible with this method are:

Collection Key Typical Path *(actual path may vary based on user configuration)
AllUsersDesktop C:\Users\Public\Desktop
AllUsersStartMenu C:\ProgramData\Microsoft\Windows\Start Menu
AllUsersPrograms C:\ProgramData\Microsoft\Windows\Start Menu\Programs
AllUsersStartup C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
AppData C:\Users\Username\AppData\Roaming
Desktop C:\Users\Username\Desktop
Favorites C:\Users\Username\Favorites
Fonts C:\Windows\Fonts
MyDocuments C:\Users\Username\Documents
NetHood C:\Users\Username\AppData\Roaming\Microsoft\Windows\Network Shortcuts
PrintHood C:\Users\Username\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
Programs C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
Recent C:\Users\Username\AppData\Roaming\Microsoft\Windows\Recent
SendTo C:\Users\Username\AppData\Roaming\Microsoft\Windows\SendTo
StartMenu C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu
Startup C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Templates C:\Users\Username\AppData\Roaming\Microsoft\Windows\Templates

2. Using the Shell Object

you can get a folder path by its ssfC (Shell Special Folder Constant). These ssfCs are synonyms for the equivalent CSIDLs. (see next chapter)

To do this, you can use the .NameSpace method of the Shell Object.

Public Function GetFolderPathBySSFC(ByVal ssfC As Long)
    Dim oShell As Object
    Set oShell = CreateObject("Shell.Application")
    
    If Not oShell.NameSpace(CVar(ssfC)) Is Nothing Then
        GetFolderPathBySSFC = oShell.NameSpace(CVar(ssfC)).Self.Path
    End If
End Function

To get the Desktop path:

Const ssfDESKTOP As Long = &H0&
desktopPath = GetFolderPathBySSFC(ssfDESKTOP)

Note:
This method can not only return paths, but also PIDLs (=ItemIdLists).
For example, GetFolderPathBySSFC(ssfPRINTERS), will return something like

"::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{2227A280-3AEA-1069-A2DE-08002B30309D}"

Again for the sake of completeness, this is how to use early binding with this Object. This time, you need a reference to the Microsoft Shell Controls And Automation library.

Public Function GetFolderPathBySSFC(ByVal ssfC As Long)
    'Requires reference to "Microsoft Shell Controls and Automation"
    Dim oShell As New Shell32.Shell
    If Not oShell.NameSpace(CVar(ssfC)) Is Nothing Then
        GetFolderPathBySSFC = oShell.NameSpace(CVar(ssfC)).Self.Path
    End If
End Function

These are the declarations of the 38 ShellSpecialFolderConstants for VBA, according to their definition in ShlDisp.h:

'Declarations of Shell Special Folder Constants for VBA
'Source: ShlDisp.h (Windows 11 SDK 10.0.22621.0) (sorted alphabetically)

'Example paths and a comparison with CSIDL can be found here:
'https://gist.github.com/guwidoe/43016947dad44457e4a90112ce32a4cf#file-csidl_vs_ssfc-md

'Registry keys:
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

Const ssfALTSTARTUP = &H1d
Const ssfAPPDATA = &H1a
Const ssfBITBUCKET = &Ha
Const ssfCOMMONALTSTARTUP = &H1e
Const ssfCOMMONAPPDATA = &H23
Const ssfCOMMONDESKTOPDIR = &H19
Const ssfCOMMONFAVORITES = &H1f
Const ssfCOMMONPROGRAMS = &H17
Const ssfCOMMONSTARTMENU = &H16
Const ssfCOMMONSTARTUP = &H18
Const ssfCONTROLS = &H3
Const ssfCOOKIES = &H21
Const ssfDESKTOP = &H0
Const ssfDESKTOPDIRECTORY = &H10
Const ssfDRIVES = &H11
Const ssfFAVORITES = &H6
Const ssfFONTS = &H14
Const ssfHISTORY = &H22
Const ssfINTERNETCACHE = &H20
Const ssfLOCALAPPDATA = &H1c
Const ssfMYPICTURES = &H27
Const ssfNETHOOD = &H13
Const ssfNETWORK = &H12
Const ssfPERSONAL = &H5
Const ssfPRINTERS = &H4
Const ssfPRINTHOOD = &H1b
Const ssfPROFILE = &H28
Const ssfPROGRAMFILES = &H26
Const ssfPROGRAMFILESx86 = &H2A 'This constant is wrongly set as &H30 in ShlDisp.h!
Const ssfPROGRAMS = &H2
Const ssfRECENT = &H8
Const ssfSENDTO = &H9
Const ssfSTARTMENU = &Hb
Const ssfSTARTUP = &H7
Const ssfSYSTEM = &H25
Const ssfSYSTEMx86 = &H29
Const ssfTEMPLATES = &H15
Const ssfWINDOWS = &H24

Note:
These ssf constants can also be used with the function from the next chapter, as CSIDL and ssfC are essentially the same thing under the hood: desktopPath = GetFolderPathByCSIDL(ssfDESKTOP)
Link - Table comparing CSIDL and SSFC, including examples for typical paths


3. Using the SHGetFolderPath API Function

you can get a folder path by its CSIDL (Shell namespace ID List).

This function can accept the same constants as the Shell object, with the caveat that this function will not return PIDLs (ItemIdLists). To get PIDLs with an API function, you would need the SHGetFolderLocation function.

To get folder paths using SHGetFolderPath, you can use this wrapper function:

Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function SHGetFolderPath Lib "shell32.dll" Alias "SHGetFolderPathA" (ByVal hWnd As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwFlags As Long, ByVal pszPath As String) As Long
#Else
    Private Declare Function SHGetFolderPath Lib "shell32.dll" Alias "SHGetFolderPathA" (ByVal hwnd As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwFlags As Long, ByVal pszPath As String) As Long
#End If

Public Function GetFolderPathByCSIDL(ByVal FolderCSIDL As Long) As String
    Const MAX_PATH As Long = 260
    Const S_OK As Long = &H0
    Dim Path As String: Path = String(MAX_PATH, vbNullChar)
    
    If SHGetFolderPath(0, FolderCSIDL, 0, 0, Path) = S_OK Then
        GetFolderPathByCSIDL = Left(Path, InStr(Path, vbNullChar) - 1)
    End If
End Function

To get the Desktop path:

Const CSIDL_DESKTOP As Long = &H0&
desktopPath = GetFolderPathByCSIDL(CSIDL_DESKTOP)

Here is a list of other available folder CSIDLs: (also available here, here, here, here, here, here, and here)

'Declarations of CSIDLs for VBA
'Source: Shlobj.h

'Example paths and a comparison with ssfCs (Shell Special Folder Constants) 
'can be found here:
'https://gist.github.com/guwidoe/43016947dad44457e4a90112ce32a4cf#file-csidl_vs_ssfc-md

'Registry keys:
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

Const CSIDL_ADMINTOOLS = &H30
Const CSIDL_ALTSTARTUP = &H1D
Const CSIDL_APPDATA = &H1A
Const CSIDL_BITBUCKET = &HA
Const CSIDL_CDBURN_AREA = &H3B
Const CSIDL_COMMON_ADMINTOOLS = &H2F
Const CSIDL_COMMON_ALTSTARTUP = &H1E
Const CSIDL_COMMON_APPDATA = &H23
Const CSIDL_COMMON_DESKTOPDIRECTORY = &H19
Const CSIDL_COMMON_DOCUMENTS = &H2E
Const CSIDL_COMMON_FAVORITES = &H1F
Const CSIDL_COMMON_MUSIC = &H35
Const CSIDL_COMMON_PICTURES = &H36
Const CSIDL_COMMON_PROGRAMS = &H17
Const CSIDL_COMMON_STARTMENU = &H16
Const CSIDL_COMMON_STARTUP = &H18
Const CSIDL_COMMON_TEMPLATES = &H2D
Const CSIDL_COMMON_VIDEO = &H37
Const CSIDL_CONTROLS = &H3
Const CSIDL_COOKIES = &H21
Const CSIDL_DESKTOP = &H0
Const CSIDL_DESKTOPDIRECTORY = &H10
Const CSIDL_DRIVES = &H11
Const CSIDL_FAVORITES = &H6
Const CSIDL_FONTS = &H14
Const CSIDL_HISTORY = &H22
Const CSIDL_INTERNET = &H1
Const CSIDL_INTERNET_CACHE = &H20
Const CSIDL_LOCAL_APPDATA = &H1C
Const CSIDL_MYDOCUMENTS = &HC
Const CSIDL_MYMUSIC = &HD
Const CSIDL_MYPICTURES = &H27
Const CSIDL_MYVIDEO = &HE
Const CSIDL_NETHOOD = &H13
Const CSIDL_NETWORK = &H12
Const CSIDL_PERSONAL = &H5
Const CSIDL_PRINTERS = &H4
Const CSIDL_PRINTHOOD = &H1B
Const CSIDL_PROFILE = &H28
Const CSIDL_PROFILES = &H3E
Const CSIDL_PROGRAM_FILES = &H26
Const CSIDL_PROGRAM_FILES_COMMON = &H2B
Const CSIDL_PROGRAMS = &H2
Const CSIDL_RECENT = &H8
Const CSIDL_SENDTO = &H9
Const CSIDL_STARTMENU = &HB
Const CSIDL_STARTUP = &H7
Const CSIDL_SYSTEM = &H25
Const CSIDL_TEMPLATES = &H15
Const CSIDL_WINDOWS = &H24

Note:
These CSIDL constants can also be used with the function from the previous chapter, as CSIDL and ssfC are essentially the same thing under the hood: desktopPath = GetFolderPathBySSFC(CSIDL_DESKTOP)
Link - Table comparing CSIDL and SSFC, including examples for typical paths


4. Using the SHGetKnownFolderPath API Function

you can get a folder path by its KnownFolderID, which is a type of CLSID (Class Identifier), which is a type of GUID (Globally Unique Identifier).

This API function, together with the KnownFolderID system, offers even more locations than the previous methods. It is the currently recommended way of finding known folder paths for applications on Windows and has been available since Windows Vista.

The following code is an adaption of this answer which is itself an adaption of the code from this original source.

Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function SHGetKnownFolderPath Lib "Shell32" (ByRef rfid As GUID, ByVal dwFlags As Long, ByVal hToken As Long, ByRef pszPath As LongPtr) As Long
    Private Declare PtrSafe Function CLSIDFromString Lib "ole32" (ByVal lpszGuid As LongPtr, ByRef pGuid As GUID) As Long
    Private Declare PtrSafe Function lstrlenW Lib "kernel32" (ByVal lpString As LongPtr) As Long
    Private Declare PtrSafe Sub CoTaskMemFree Lib "ole32" (ByVal hMem As LongPtr)
    Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As LongPtr, ByVal Source As LongPtr, ByVal length As Long)
#Else
    Private Declare Function SHGetKnownFolderPath Lib "shell32" (rfid As Any, ByVal dwFlags As Long, ByVal hToken As Long, ppszPath As Long) As Long
    Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpszGuid As Long, pGuid As Any) As Long
    Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
    Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal pv As Long)
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long)
#End If

Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
End Type

#If VBA7 Then
Private Function GetBstrFromWideStringPtr(ByVal lpwString As LongPtr) As String
#Else
Private Function GetBstrFromWideStringPtr(ByVal lpwString As Long) As String
#End If
    Dim length As Long
    If (lpwString) Then length = lstrlenW(lpwString)
    If (length) Then
        GetBstrFromWideStringPtr = Space$(length)
        CopyMemory StrPtr(GetBstrFromWideStringPtr), lpwString, length * 2
    End If
End Function

Public Function GetPathByKnownFolderID(ByVal KnownFolderID As String) As String
    Const S_OK As Long = &H0
    Const WIN32_NULL As Long = &H0
    Dim ref As GUID
    #If VBA7 Then
        Dim pszPath As LongPtr
    #Else
        Dim pszPath As Long
    #End If

    If (CLSIDFromString(StrPtr(KnownFolderID), ref) = S_OK) Then
        If (SHGetKnownFolderPath(ref, 0, WIN32_NULL, pszPath) = S_OK) Then
            GetPathByKnownFolderID = GetBstrFromWideStringPtr(pszPath)
            CoTaskMemFree pszPath
        End If
    End If
End Function

To get the Desktop path:

Const FOLDERID_Desktop As String = "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}"
desktopPath = GetPathByKnownFolderID(FOLDERID_Desktop)

Here is a list of other available KnownFolderIDs:

'Declarations of standard KnownFolderIDs for VBA
'Source: KnownFolders.h (Windows 11 SDK 10.0.22621.0) (sorted alphabetically)

'Example paths for most of these KnownFolderIDs can be found under this link:
'https://gist.github.com/guwidoe/43016947dad44457e4a90112ce32a4cf#file-examplepathsforknownfolderids-md

'Registry key:
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions

Const FOLDERID_AccountPictures        As String = "{008ca0b1-55b4-4c56-b8a8-4de4b299d3be}"
Const FOLDERID_AddNewPrograms         As String = "{de61d971-5ebc-4f02-a3a9-6c82895e5c04}"
Const FOLDERID_AdminTools             As String = "{724EF170-A42D-4FEF-9F26-B60E846FBA4F}"
Const FOLDERID_AllAppMods             As String = "{7ad67899-66af-43ba-9156-6aad42e6c596}"
Const FOLDERID_AppCaptures            As String = "{EDC0FE71-98D8-4F4A-B920-C8DC133CB165}"
Const FOLDERID_AppDataDesktop         As String = "{B2C5E279-7ADD-439F-B28C-C41FE1BBF672}"
Const FOLDERID_AppDataDocuments       As String = "{7BE16610-1F7F-44AC-BFF0-83E15F2FFCA1}"
Const FOLDERID_AppDataFavorites       As String = "{7CFBEFBC-DE1F-45AA-B843-A542AC536CC9}"
Const FOLDERID_AppDataProgramData     As String = "{559D40A3-A036-40FA-AF61-84CB430A4D34}"
Const FOLDERID_ApplicationShortcuts   As String = "{A3918781-E5F2-4890-B3D9-A7E54332328C}"
Const FOLDERID_AppsFolder             As String = "{1e87508d-89c2-42f0-8a7e-645a0f50ca58}"
Const FOLDERID_AppUpdates             As String = "{a305ce99-f527-492b-8b1a-7e76fa98d6e4}"
Const FOLDERID_CameraRoll             As String = "{AB5FB87B-7CE2-4F83-915D-550846C9537B}"
Const FOLDERID_CameraRollLibrary      As String = "{2B20DF75-1EDA-4039-8097-38798227D5B7}"
Const FOLDERID_CDBurning              As String = "{9E52AB10-F80D-49DF-ACB8-4330F5687855}"
Const FOLDERID_ChangeRemovePrograms   As String = "{df7266ac-9274-4867-8d55-3bd661de872d}"
Const FOLDERID_CommonAdminTools       As String = "{D0384E7D-BAC3-4797-8F14-CBA229B392B5}"
Const FOLDERID_CommonOEMLinks         As String = "{C1BAE2D0-10DF-4334-BEDD-7AA20B227A9D}"
Const FOLDERID_CommonPrograms         As String = "{0139D44E-6AFE-49F2-8690-3DAFCAE6FFB8}"
Const FOLDERID_CommonStartMenu        As String = "{A4115719-D62E-491D-AA7C-E74B8BE3B067}"
Const FOLDERID_CommonStartMenuPlaces  As String = "{A440879F-87A0-4F7D-B700-0207B966194A}"
Const FOLDERID_CommonStartup          As String = "{82A5EA35-D9CD-47C5-9629-E15D2F714E6E}"
Const FOLDERID_CommonTemplates        As String = "{B94237E7-57AC-4347-9151-B08C6C32D1F7}"
Const FOLDERID_ComputerFolder         As String = "{0AC0837C-BBF8-452A-850D-79D08E667CA7}"
Const FOLDERID_ConflictFolder         As String = "{4bfefb45-347d-4006-a5be-ac0cb0567192}"
Const FOLDERID_ConnectionsFolder      As String = "{6F0CD92B-2E97-45D1-88FF-B0D186B8DEDD}"
Const FOLDERID_Contacts               As String = "{56784854-C6CB-462b-8169-88E350ACB882}"
Const FOLDERID_ControlPanelFolder     As String = "{82A74AEB-AEB4-465C-A014-D097EE346D63}"
Const FOLDERID_Cookies                As String = "{2B0F765D-C0E9-4171-908E-08A611B84FF6}"
Const FOLDERID_CurrentAppMods         As String = "{3db40b20-2a30-4dbe-917e-771dd21dd099}"
Const FOLDERID_Desktop                As String = "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}"
Const FOLDERID_DevelopmentFiles       As String = "{DBE8E08E-3053-4BBC-B183-2A7B2B191E59}"
Const FOLDERID_Device                 As String = "{1C2AC1DC-4358-4B6C-9733-AF21156576F0}"
Const FOLDERID_DeviceMetadataStore    As String = "{5CE4A5E9-E4EB-479D-B89F-130C02886155}"
Const FOLDERID_Documents              As String = "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}"
Const FOLDERID_DocumentsLibrary       As String = "{7b0db17d-9cd2-4a93-9733-46cc89022e7c}"
Const FOLDERID_Downloads              As String = "{374DE290-123F-4565-9164-39C4925E467B}"
Const FOLDERID_Favorites              As String = "{1777F761-68AD-4D8A-87BD-30B759FA33DD}"
Const FOLDERID_Fonts                  As String = "{FD228CB7-AE11-4AE3-864C-16F3910AB8FE}"
Const FOLDERID_Games                  As String = "{CAC52C1A-B53D-4edc-92D7-6B2E8AC19434}"
Const FOLDERID_GameTasks              As String = "{054FAE61-4DD8-4787-80B6-090220C4B700}"
Const FOLDERID_History                As String = "{D9DC8A3B-B784-432E-A781-5A1130A75963}"
Const FOLDERID_HomeGroup              As String = "{52528A6B-B9E3-4add-B60D-588C2DBA842D}"
Const FOLDERID_HomeGroupCurrentUser   As String = "{9B74B6A3-0DFD-4f11-9E78-5F7800F2E772}"
Const FOLDERID_ImplicitAppShortcuts   As String = "{bcb5256f-79f6-4cee-b725-dc34e402fd46}"
Const FOLDERID_InternetCache          As String = "{352481E8-33BE-4251-BA85-6007CAEDCF9D}"
Const FOLDERID_InternetFolder         As String = "{4D9F7874-4E0C-4904-967B-40B0D20C3E4B}"
Const FOLDERID_Libraries              As String = "{1B3EA5DC-B587-4786-B4EF-BD1DC332AEAE}"
Const FOLDERID_Links                  As String = "{bfb9d5e0-c6a9-404c-b2b2-ae6db6af4968}"
Const FOLDERID_LocalAppData           As String = "{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}"
Const FOLDERID_LocalAppDataLow        As String = "{A520A1A4-1780-4FF6-BD18-167343C5AF16}"
Const FOLDERID_LocalDocuments         As String = "{f42ee2d3-909f-4907-8871-4c22fc0bf756}"
Const FOLDERID_LocalDownloads         As String = "{7d83ee9b-2244-4e70-b1f5-5393042af1e4}"
Const FOLDERID_LocalizedResourcesDir  As String = "{2A00375E-224C-49DE-B8D1-440DF7EF3DDC}"
Const FOLDERID_LocalMusic             As String = "{a0c69a99-21c8-4671-8703-7934162fcf1d}"
Const FOLDERID_LocalPictures          As String = "{0ddd015d-b06c-45d5-8c4c-f59713854639}"
Const FOLDERID_LocalStorage           As String = "{B3EB08D3-A1F3-496B-865A-42B536CDA0EC}"
Const FOLDERID_LocalVideos            As String = "{35286a68-3c57-41a1-bbb1-0eae73d76c95}"
Const FOLDERID_Music                  As String = "{4BD8D571-6D19-48D3-BE97-422220080E43}"
Const FOLDERID_MusicLibrary           As String = "{2112AB0A-C86A-4ffe-A368-0DE96E47012E}"
Const FOLDERID_NetHood                As String = "{C5ABBF53-E17F-4121-8900-86626FC2C973}"
Const FOLDERID_NetworkFolder          As String = "{D20BEEC4-5CA8-4905-AE3B-BF251EA09B53}"
Const FOLDERID_Objects3D              As String = "{31C0DD25-9439-4F12-BF41-7FF4EDA38722}"
Const FOLDERID_OneDrive               As String = "{A52BBA46-E9E1-435f-B3D9-28DAA648C0F6}"
Const FOLDERID_OriginalImages         As String = "{2C36C0AA-5812-4b87-BFD0-4CD0DFB19B39}"
Const FOLDERID_PhotoAlbums            As String = "{69D2CF90-FC33-4FB7-9A0C-EBB0F0FCB43C}"
Const FOLDERID_Pictures               As String = "{33E28130-4E1E-4676-835A-98395C3BC3BB}"
Const FOLDERID_PicturesLibrary        As String = "{A990AE9F-A03B-4e80-94BC-9912D7504104}"
Const FOLDERID_Playlists              As String = "{DE92C1C7-837F-4F69-A3BB-86E631204A23}"
Const FOLDERID_PrintersFolder         As String = "{76FC4E2D-D6AD-4519-A663-37BD56068185}"
Const FOLDERID_PrintHood              As String = "{9274BD8D-CFD1-41C3-B35E-B13F55A758F4}"
Const FOLDERID_Profile                As String = "{5E6C858F-0E22-4760-9AFE-EA3317B67173}"
Const FOLDERID_ProgramData            As String = "{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}"
Const FOLDERID_ProgramFiles           As String = "{905e63b6-c1bf-494e-b29c-65b732d3d21a}"
Const FOLDERID_ProgramFilesCommon     As String = "{F7F1ED05-9F6D-47A2-AAAE-29D317C6F066}"
Const FOLDERID_ProgramFilesCommonX64  As String = "{6365D5A7-0F0D-45e5-87F6-0DA56B6A4F7D}"
Const FOLDERID_ProgramFilesCommonX86  As String = "{DE974D24-D9C6-4D3E-BF91-F4455120B917}"
Const FOLDERID_ProgramFilesX64        As String = "{6D809377-6AF0-444b-8957-A3773F02200E}"
Const FOLDERID_ProgramFilesX86        As String = "{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}"
Const FOLDERID_Programs               As String = "{A77F5D77-2E2B-44C3-A6A2-ABA601054A51}"
Const FOLDERID_Public                 As String = "{DFDF76A2-C82A-4D63-906A-5644AC457385}"
Const FOLDERID_PublicDesktop          As String = "{C4AA340D-F20F-4863-AFEF-F87EF2E6BA25}"
Const FOLDERID_PublicDocuments        As String = "{ED4824AF-DCE4-45A8-81E2-FC7965083634}"
Const FOLDERID_PublicDownloads        As String = "{3D644C9B-1FB8-4f30-9B45-F670235F79C0}"
Const FOLDERID_PublicGameTasks        As String = "{DEBF2536-E1A8-4c59-B6A2-414586476AEA}"
Const FOLDERID_PublicLibraries        As String = "{48daf80b-e6cf-4f4e-b800-0e69d84ee384}"
Const FOLDERID_PublicMusic            As String = "{3214FAB5-9757-4298-BB61-92A9DEAA44FF}"
Const FOLDERID_PublicPictures         As String = "{B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5}"
Const FOLDERID_PublicRingtones        As String = "{E555AB60-153B-4D17-9F04-A5FE99FC15EC}"
Const FOLDERID_PublicUserTiles        As String = "{0482af6c-08f1-4c34-8c90-e17ec98b1e17}"
Const FOLDERID_PublicVideos           As String = "{2400183A-6185-49FB-A2D8-4A392A602BA3}"
Const FOLDERID_QuickLaunch            As String = "{52a4f021-7b75-48a9-9f6b-4b87a210bc8f}"
Const FOLDERID_Recent                 As String = "{AE50C081-EBD2-438A-8655-8A092E34987A}"
Const FOLDERID_RecordedCalls          As String = "{2f8b40c2-83ed-48ee-b383-a1f157ec6f9a}"
Const FOLDERID_RecordedTVLibrary      As String = "{1A6FDBA2-F42D-4358-A798-B74D745926C5}"
Const FOLDERID_RecycleBinFolder       As String = "{B7534046-3ECB-4C18-BE4E-64CD4CB7D6AC}"
Const FOLDERID_ResourceDir            As String = "{8AD10C31-2ADB-4296-A8F7-E4701232C972}"
Const FOLDERID_RetailDemo             As String = "{12D4C69E-24AD-4923-BE19-31321C43A767}"
Const FOLDERID_Ringtones              As String = "{C870044B-F49E-4126-A9C3-B52A1FF411E8}"
Const FOLDERID_RoamedTileImages       As String = "{AAA8D5A5-F1D6-4259-BAA8-78E7EF60835E}"
Const FOLDERID_RoamingAppData         As String = "{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}"
Const FOLDERID_RoamingTiles           As String = "{00BCFC5A-ED94-4e48-96A1-3F6217F21990}"
Const FOLDERID_SampleMusic            As String = "{B250C668-F57D-4EE1-A63C-290EE7D1AA1F}"
Const FOLDERID_SamplePictures         As String = "{C4900540-2379-4C75-844B-64E6FAF8716B}"
Const FOLDERID_SamplePlaylists        As String = "{15CA69B3-30EE-49C1-ACE1-6B5EC372AFB5}"
Const FOLDERID_SampleVideos           As String = "{859EAD94-2E85-48AD-A71A-0969CB56A6CD}"
Const FOLDERID_SavedGames             As String = "{4C5C32FF-BB9D-43b0-B5B4-2D72E54EAAA4}"
Const FOLDERID_SavedPictures          As String = "{3B193882-D3AD-4eab-965A-69829D1FB59F}"
Const FOLDERID_SavedPicturesLibrary   As String = "{E25B5812-BE88-4bd9-94B0-29233477B6C3}"
Const FOLDERID_SavedSearches          As String = "{7d1d3a04-debb-4115-95cf-2f29da2920da}"
Const FOLDERID_Screenshots            As String = "{b7bede81-df94-4682-a7d8-57a52620b86f}"
Const FOLDERID_SEARCH_CSC             As String = "{ee32e446-31ca-4aba-814f-a5ebd2fd6d5e}"
Const FOLDERID_SEARCH_MAPI            As String = "{98ec0e18-2098-4d44-8644-66979315a281}"
Const FOLDERID_SearchHistory          As String = "{0D4C3DB6-03A3-462F-A0E6-08924C41B5D4}"
Const FOLDERID_SearchHome             As String = "{190337d1-b8ca-4121-a639-6d472d16972a}"
Const FOLDERID_SearchTemplates        As String = "{7E636BFE-DFA9-4D5E-B456-D7B39851D8A9}"
Const FOLDERID_SendTo                 As String = "{8983036C-27C0-404B-8F08-102D10DCFD74}"
Const FOLDERID_SidebarDefaultParts    As String = "{7B396E54-9EC5-4300-BE0A-2482EBAE1A26}"
Const FOLDERID_SidebarParts           As String = "{A75D362E-50FC-4fb7-AC2C-A8BEAA314493}"
Const FOLDERID_SkyDrive               As String = "{A52BBA46-E9E1-435f-B3D9-28DAA648C0F6}"
Const FOLDERID_SkyDriveCameraRoll     As String = "{767E6811-49CB-4273-87C2-20F355E1085B}"
Const FOLDERID_SkyDriveDocuments      As String = "{24D89E24-2F19-4534-9DDE-6A6671FBB8FE}"
Const FOLDERID_SkyDriveMusic          As String = "{C3F2459E-80D6-45DC-BFEF-1F769F2BE730}"
Const FOLDERID_SkyDrivePictures       As String = "{339719B5-8C47-4894-94C2-D8F77ADD44A6}"
Const FOLDERID_StartMenu              As String = "{625B53C3-AB48-4EC1-BA1F-A1EF4146FC19}"
Const FOLDERID_StartMenuAllPrograms   As String = "{F26305EF-6948-40B9-B255-81453D09C785}"
Const FOLDERID_Startup                As String = "{B97D20BB-F46A-4C97-BA10-5E3608430854}"
Const FOLDERID_SyncManagerFolder      As String = "{43668BF8-C14E-49B2-97C9-747784D784B7}"
Const FOLDERID_SyncResultsFolder      As String = "{289a9a43-be44-4057-a41b-587a76d7e7f9}"
Const FOLDERID_SyncSetupFolder        As String = "{0F214138-B1D3-4a90-BBA9-27CBC0C5389A}"
Const FOLDERID_System                 As String = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}"
Const FOLDERID_SystemX86              As String = "{D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}"
Const FOLDERID_Templates              As String = "{A63293E8-664E-48DB-A079-DF759E0509F7}"
Const FOLDERID_UserPinned             As String = "{9e3995ab-1f9c-4f13-b827-48b24b6c7174}"
Const FOLDERID_UserProfiles           As String = "{0762D272-C50A-4BB0-A382-697DCD729B80}"
Const FOLDERID_UserProgramFiles       As String = "{5cd7aee2-2219-4a67-b85d-6c9ce15660cb}"
Const FOLDERID_UserProgramFilesCommon As String = "{bcbd3057-ca5c-4622-b42d-bc56db0ae516}"
Const FOLDERID_UsersFiles             As String = "{f3ce0f7c-4901-4acc-8648-d5d44b04ef8f}"
Const FOLDERID_UsersLibraries         As String = "{A302545D-DEFF-464b-ABE8-61C8648D939B}"
Const FOLDERID_Videos                 As String = "{18989B1D-99B5-455B-841C-AB7C74E4DDFC}"
Const FOLDERID_VideosLibrary          As String = "{491E922F-5643-4af4-A7EB-4E7A138D8174}"
Const FOLDERID_Windows                As String = "{F38BF404-1D43-42F2-9305-67DE0B28FC23}"

Link - Example paths for KnownFolderIDs

GWD
  • 3,081
  • 14
  • 30
  • 1
    Possibly helpful, too: [Getting .. folders .. using the Environ function](https://www.codevba.com/office/environ.htm#AppDataRoaming). – T.M. Jan 13 '23 at 19:40
  • @T.M. I tried making this post as complete as possible for future reference. Unfortunately, now it is almost at the 30000 character limit and therefore I can't add the `Environ` method anymore. Maybe I'll edit it in the future to mention it... – GWD Jan 15 '23 at 01:13
  • Why not a 2nd answer mentioning the 30000 character limit? @GWD – T.M. Jan 15 '23 at 09:21
  • @T.M. it also doesn't really fit this question because op specifically wrote it doesn't work in this case and I agree with him. There is no reliable way of getting the *Desktop* path using `Environ`... – GWD Jan 15 '23 at 11:03
  • I don't want to force it on you, but it would be a chance to also show why eg `Environ("USERPROFILE") & "\Desktop\"` is not reliable. – T.M. Jan 15 '23 at 17:30