download<\/a> an Excel Workbook with code to play around with the code.<\/p>\n[checklist icon=”” iconcolor=”” circle=”” circlecolor=”” size=”18px” class=”” id=””][li_item icon=”fa-book”]How to get path of My Documents<\/strong> in Excel VBA[\/li_item][li_item icon=”fa-desktop”]How to get path of Desktop<\/strong> in Excel VBA[\/li_item][li_item icon=”fa-laptop”]3. How to get path of All User Desktop<\/strong> in Excel VBA[\/li_item][li_item icon=”fa-file-text-o”]How to get path of Recent Documents<\/strong> in Excel VBA[\/li_item][li_item icon=”fa-folder-open-o”]How to get the path of Favorites<\/strong> folder in Excel VBA[\/li_item][li_item icon=”fa-folder-open”]How to get the path of Programs Folder<\/strong> in Excel VBA.[\/li_item][li_item icon=”fa-windows”]How to get the path of Start Menu Folder<\/strong> in Excel VBA
\n[\/li_item][li_item icon=”fa-share-square-o”]How to get the path of Send To Folder<\/strong> in Excel VBA[\/li_item][\/checklist]<\/p>\nThere are three ways of finding the above path in Excel VBA.<\/p>\n
1. Using WScript.Shell<\/strong><\/h2>\n2. Using Windows shfolder.dll<\/strong><\/h2>\n3. Using Excel VBA Function Environ$<\/strong><\/h2>\n <\/p>\n
1. How to get path of Special folders in Windows using Excel VBA<\/h1>\n\r\n\r\nSub GetSpecialFolderPath()\r\nDim objSFolders As Object\r\nSet objSFolders = CreateObject("WScript.Shell").SpecialFolders\r\nSheets("Sheet1").Activate\r\nWith Sheets("Sheet1")\r\n.Range("B2").Value = "My Document Path is:- " & objSFolders("mydocuments")\r\n.Range("B3").Value = "Desktop Path is:- " & objSFolders("desktop")\r\n.Range("B4").Value = "All User Desktop Path is:- " & objSFolders("allusersdesktop")\r\n.Range("B5").Value = "Recent Documents Path is:- " & objSFolders("recent")\r\n.Range("B6").Value = "Favorites Document Path is:- " & objSFolders("favorites")\r\n.Range("B7").Value = "Programs Path is:- " & objSFolders("programs")\r\n.Range("B8").Value = "Start Menu Path is:- " & objSFolders("StartMenu")\r\n.Range("B9").Value = "Send To Path is:- " & objSFolders("SendTo")\r\nEnd With\r\nEnd Sub\r\n\r\n<\/code><\/pre>\n2. Get Special Folder Path using shfolder.dll<\/h1>\n
About shfolder.dll:<\/strong><\/span>The shfolder.dll module is the DLL for shell folder services. The functions of the shfolder.dll include displaying of Windows special folders such as Desktop, My Documents, Programs<\/strong> etc. If this DLL is missing or disabled then accessing these folders is not possible.
\nBelow is the Function which returns the Special Folder Path:<\/p>\n\r\n'***********************************\r\n' Function to get the special\r\n' folder path using WScript.Shell\r\n'***********************************\r\n\r\nPrivate Declare Function GetFolderPath Lib "shfolder.dll" _\r\n Alias "SHGetFolderPathA" _\r\n (ByVal hwndOwner As Long, _\r\n ByVal nFolder As Long, _\r\n ByVal hToken As Long, _\r\n ByVal dwReserved As Long, _\r\n ByVal lpszPath As String) As Long\r\n<\/code><\/pre>\nNow we can call the above function to get the special folder path by passing the above mentioned parameters in the function. For Different folders all you need to change is hwndOwner<\/strong> and nFolder<\/strong> values while calling.<\/p>\n\r\n'**********************************\r\n' Function to get the Folder Path\r\n' using shfolder.dll\r\n'**********************************\r\nFunction GetSFolderPath()\r\n Dim sBuffer As String\r\n sBuffer = Space$(260)\r\n 'To get the My Documents Path\r\n If GetFolderPath(&H5, &H5, -1, SHGFP_TYPE_default, sBuffer) = 0 Then\r\n Sheet1.Range("D2").Value = Left$(sBuffer, StrPtr(sBuffer))\r\n End If\r\n ' To get the Desktop Path\r\n If GetFolderPath(&H10, &H10, -1, SHGFP_TYPE_default, sBuffer) = 0 Then\r\n Sheet1.Range("D3").Value = Left$(sBuffer, StrPtr(sBuffer))\r\n End If\r\n ' To get the All User Desktop Path\r\n If GetFolderPath(&H19, &H19, -1, SHGFP_TYPE_default, sBuffer) = 0 Then\r\n Sheet1.Range("D4").Value = Left$(sBuffer, StrPtr(sBuffer))\r\n End If \r\n ' To get the Recent Document Path\r\n If GetFolderPath(&H8, &H8, -1, SHGFP_TYPE_default, sBuffer) = 0 Then\r\n Sheet1.Range("D5").Value = Left$(sBuffer, StrPtr(sBuffer))\r\n End If\r\n ' To get the Favorites Path\r\n If GetFolderPath(&H6, &H6, -1, SHGFP_TYPE_default, sBuffer) = 0 Then\r\n Sheet1.Range("D6").Value = Left$(sBuffer, StrPtr(sBuffer))\r\n End If\r\n ' To get the Program Path\r\n If GetFolderPath(&H2, &H2, -1, SHGFP_TYPE_default, sBuffer) = 0 Then\r\n Sheet1.Range("D7").Value = Left$(sBuffer, StrPtr(sBuffer))\r\n End If\r\n ' To get the Start Menu Path\r\n If GetFolderPath(&HB, &HB, -1, SHGFP_TYPE_default, sBuffer) = 0 Then\r\n Sheet1.Range("D8").Value = Left$(sBuffer, StrPtr(sBuffer))\r\n End If\r\n ' To get the Send To Path\r\n If GetFolderPath(&H9, &H9, -1, SHGFP_TYPE_default, sBuffer) = 0 Then\r\n Sheet1.Range("D9").Value = Left$(sBuffer, StrPtr(sBuffer))\r\n End If\r\nEnd Function\r\n<\/code><\/pre>\n