Find users special folders (such as desktop/My documents)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    Find users special folders (such as desktop/My documents)

    It can often be usefull to know what the path is to the users desktop, or his/her My Documents folder. The problem is that these folder paths can be changed by the user. However windows comes with built in support for accessing these values. The function here will let you get that information quite easy.

    This will also work regardless of the language setup on the users computer (which is important since this affects the naming of some folders like My Documents.

    Code:
    Public Function SpecialFolderPath(strFolder As String) As String
        ' Find out the path to the passed special folder. User on of the following arguments:
        ' Options For specical folders
    '        AllUsersDesktop
    '        AllUsersStartMenu
    '        AllUsersPrograms
    '        AllUsersStartup
    '        Desktop
    '        Favorites
    '        Fonts
    '        MyDocuments
    '        NetHood
    '        PrintHood
    '        Programs
    '        Recent
    '        SendTo
    '        StartMenu
    '        Startup
    '        Templates
        
       On Error GoTo ErrorHandler
        
       'Create a Windows Script Host Object
          Dim objWSHShell As Object
          Set objWSHShell = CreateObject("WScript.Shell")
    
       'Retrieve path
          SpecialFolderPath = objWSHShell.SpecialFolders(strFolder & "")
          
       ' Clean up
          Set objWSHShell = Nothing
          Exit Function
    
    '**************************************
    '*      Error Handler
    '**************************************
    ErrorHandler:
        MsgBox "Error finding " & strFolder, vbCritical + vbOKOnly, "Error"
    End Function
Working...