VBA Tipp: Spezielle Verzeichnisse ermitteln

Aus DBWiki
Wechseln zu: Navigation, Suche

Anforderung

Ich möchte die tatsächlichen Pfade für spezielle Verzeichnisse wie "Startmenü" oder "Favoriten" oder "Eigene Bilder" ermitteln.

  • Lösung 1:
    • Verwendung der API-Funktion SHGetFolderPath (ab Windows 95) mit CSIDL-Konstanten (constant special item ID list).
    • Die Verzeichnisse werden als "Special Folders" bezeichnet.
    • Die Funktion wird inzwischen von Microsoft als "deprecated" (veraltet) eingestuft.
  • Lösung 2:
    • Verwendung der API-Funktion SHGetKnownFolderPath (ab Windows Vista) mit GUID-Konstanten (Globally Unique Identifier).
    • Die Verzeichnisse werden als "Known Folders" bezeichnet.


Lösung 1

Das geht mit der API-Funktion SHGetFolderPath und der Funktion GetSpecFolder, die in einem globalen Modul hinterlegt werden. (auch für Windows-Versionen < Vista geeignet)

Private Const CSIDL_FLAG_CREATE            As Long = &H8000
 
Public Const CSIDL_DESKTOP                 As Long = &H0   ' Desktop
Public Const CSIDL_INTERNET                As Long = &H1   ' Internet (Icon auf Desktop)
Public Const CSIDL_PROGRAMS                As Long = &H2   ' Startmenü: Programme
Public Const CSIDL_CONTROLS                As Long = &H3   ' Systemsteuerung
Public Const CSIDL_PRINTERS                As Long = &H4   ' Drucker
Public Const CSIDL_PERSONAL                As Long = &H5   ' Eigene Dateien
Public Const CSIDL_FAVORITES               As Long = &H6   ' Benutzername\Favoriten
Public Const CSIDL_STARTUP                 As Long = &H7   ' Startmenü\Programme\Autostart
Public Const CSIDL_RECENT                  As Long = &H8   ' Benutzername\Zuletzt benutzte Dokumente
Public Const CSIDL_SENDTO                  As Long = &H9   ' Benutzername\Senden an
Public Const CSIDL_BITBUCKET               As Long = &HA   ' Desktop\Papierkorb
Public Const CSIDL_STARTMENU               As Long = &HB   ' Benutzername\Startmenü
Public Const CSIDL_MYMUSIC                 As Long = &HD  ' "Meine Music" Verzeichnis
Public Const CSIDL_MYVIDEO                 As Long = &HE  ' "Meine Videos" Verzeichnis
Public Const CSIDL_DESKTOPDIRECTORY        As Long = &H10  ' Benutzername\Desktopverzeichnis
Public Const CSIDL_DRIVES                  As Long = &H11  ' Mein Computer
Public Const CSIDL_NETWORK                 As Long = &H12  ' Netzwerk
Public Const CSIDL_NETHOOD                 As Long = &H13  ' Benutzername\Netzwerkumgebung
Public Const CSIDL_FONTS                   As Long = &H14  ' Fonts
Public Const CSIDL_TEMPLATES               As Long = &H15  ' Vorlagen
Public Const CSIDL_COMMON_STARTMENU        As Long = &H16  ' "All Users"-Profil - Startmenü
Public Const CSIDL_COMMON_PROGRAMS         As Long = &H17  ' "All Users"-Profil - Programme
Public Const CSIDL_COMMON_STARTUP          As Long = &H18  ' "All Users"-Profil - Autostart
Public Const CSIDL_COMMON_DESKTOPDIRECTORY As Long = &H19  ' Desktopverzeichnis (Allgemein)
Public Const CSIDL_APPDATA                 As Long = &H1A  ' Benutzername\Anwendungsdaten
Public Const CSIDL_PRINTHOOD               As Long = &H1B  ' Benutzername\Druckumgebung
Public Const CSIDL_LOCAL_APPDATA           As Long = &H1C  ' Benutzername\AppData\Local
Public Const CSIDL_ALTSTARTUP              As Long = &H1D  ' non localized startup
Public Const CSIDL_COMMON_ALTSTARTUP       As Long = &H1E  ' non localized common startup
Public Const CSIDL_COMMON_FAVORITES        As Long = &H1F
Public Const CSIDL_INTERNET_CACHE          As Long = &H20  ' Temporäre Internetdateien (MSIE)
Public Const CSIDL_COOKIES                 As Long = &H21  ' Cookies (MSIE)
Public Const CSIDL_HISTORY                 As Long = &H22  ' Verlauf (MSIE)
Public Const CSIDL_COMMON_APPDATA          As Long = &H23  ' Anwendungsdaten (Allgemein)
Public Const CSIDL_WINDOWS                 As Long = &H24  ' Windows
Public Const CSIDL_SYSTEM                  As Long = &H25  ' Windows-System
Public Const CSIDL_PROGRAM_FILES           As Long = &H26  ' Programme
Public Const CSIDL_MYPICTURES              As Long = &H27  ' Eigene Bilder
Public Const CSIDL_PROFILE                 As Long = &H28
Public Const CSIDL_SYSTEMX86               As Long = &H29
Public Const CSIDL_PROGRAM_FILESX86        As Long = &H2A
Public Const CSIDL_PROGRAM_FILES_COMMON    As Long = &H2B  ' Gemeinsame Dateien
Public Const CSIDL_PROGRAM_FILES_COMMONX86 As Long = &H2C
Public Const CSIDL_COMMON_TEMPLATES        As Long = &H2D  ' Vorlagen (Allgemein)
Public Const CSIDL_COMMON_DOCUMENTS        As Long = &H2E  ' Dokumente (Allgemein)
Public Const CSIDL_COMMON_ADMINTOOLS       As Long = &H2F  ' Verwaltung (Allgemein)
Public Const CSIDL_ADMINTOOLS              As Long = &H30
Public Const CSIDL_CONNECTIONS             As Long = &H31
Public Const CSIDL_FOLDER_MASK             As Long = &HFF
Public Const CSIDL_COMMON_MUSIC            As Long = &H35   ' All Users\My Music
Public Const CSIDL_COMMON_PICTURES         As Long = &H36   ' All Users\My Pictures
Public Const CSIDL_COMMON_VIDEO            As Long = &H37   ' All Users\My Video
Public Const CSIDL_RESOURCES               As Long = &H38   ' Resource Direcotry
Public Const CSIDL_RESOURCES_LOCALIZED     As Long = &H39   ' Localized Resource Direcotry
Public Const CSIDL_COMMON_OEM_LINKS        As Long = &H3A   ' Links to All Users OEM specific apps
Public Const CSIDL_CDBURN_AREA             As Long = &H3B   ' USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
Public Const CSIDL_COMPUTERSNEARME         As Long = &H3D   ' Computers Near Me (computered from Workgroup membership)
 
Private Const CSIDL_FLAG_DONT_VERIFY As Long = &H4000
Private Const CSIDL_FLAG_MASK        As Long = &HFF00
 
Private Const SHGFP_TYPE_CURRENT As Long = 0
Private Const SHGFP_TYPE_DEFAULT As Long = 1
Private Const MAX_PATH           As Long = 260
 
#If 0 Then
Note: As of Windows Vista, this function is merely a wrapper for
      SHGetKnownFolderPath.  The CSIDL value is translated to its
      associated KNOWNFOLDERID and then SHGetKnownFolderPath is
      called.  New applications should use the known folder system
      rather than the older CSIDL system, which is supported only
      for backward compatibility.
 
Anmerkung: Ab Windows Vista ist diese Funktion lediglich ein Wrapper für
           SHGetKnownFolderPath.  Der CSIDL-Wert wird in seinen zugehörigen
           Wert der KNOWNFOLDERID umgewandelt, und es wird SHGetKnownFolderPath
           aufgerufen.  Neue Anwendungen sollten das bekannte Ordnersystem
           und nicht das ältere CSIDL-System verwenden, das nur der Abwärts-
           kompatibilität wegen unterstützt wird.
#End If
 
#If VBA7 Then
Private Declare PtrSafe Function SHGetFolderPathW Lib "shfolder" ( _
   ByVal hwndOwner As LongPtr, _
   ByVal nFolder As Long, _
   ByVal hToken As LongPtr, _
   ByVal dwFlags As Long, _
   ByVal pszPath As LongPtr) As Long
#Else  ' VBA6 (für Office Versionen <= 2007)
Private Declare Function SHGetFolderPathW Lib "shfolder" ( _
   ByVal hwndOwner As Long, _
   ByVal nFolder As Long, _
   ByVal hToken As Long, _
   ByVal dwFlags As Long, _
   ByVal pszPath As Long) As Long
#End If
 
Public Function GetSpecFolder(lCSIDL As Long, _
                              Optional bCreate As Boolean, _
                              Optional bVerify As Boolean) As String
 
   ' Quelle: www.dbwiki.net oder www.dbwiki.de
 
   Dim buf    As String
   Dim hRes   As Long
   Dim lFlags As Long
 
   lFlags = lCSIDL
   If bCreate Then lFlags = lFlags Or CSIDL_FLAG_CREATE
   If Not bCreate Then lFlags = lFlags Or CSIDL_FLAG_DONT_VERIFY
 
   ' Puffer füllen
   buf = String$(MAX_PATH, vbNullChar)
   hRes = SHGetFolderPathW(0, lFlags, 0, SHGFP_TYPE_CURRENT, StrPtr(buf))
   Select Case hRes
      Case 0
         ' Verzeichnis gefunden
         GetSpecFolder = Left$(buf, InStr(1, buf, vbNullChar) - 1)
      Case Else
         Err.Raise hRes
   End Select
End Function

Aufruf

Den "Programme"-Ordner im Startmenü erhält man mit:

   MsgBox GetSpecFolder(CSIDL_PROGRAMS)

Lösung 2

Das geht mit der API-Funktion SHGetKnownFolderPath, weiteren API-Hilfsfunktionen, und der Funktion GetFolder, die in einem globalen Modul hinterlegt werden.

Wiki hinweis.png

Anmerkung: Nur unter Betriebssystem Windows Vista und höher verwendbar.


Todo: 64-Bit-kompatibel gestalten

Public Const FOLDERID_NetworkFolder                As String = "{D20BEEC4-5CA8-4905-AE3B-BF251EA09B53}"
 
Public Const FOLDERID_ComputerFolder               As String = "{0AC0837C-BBF8-452A-850D-79D08E667CA7}"
Public Const FOLDERID_InternetFolder               As String = "{4D9F7874-4E0C-4904-967B-40B0D20C3E4B}"
Public Const FOLDERID_ControlPanelFolder           As String = "{82A74AEB-AEB4-465C-A014-D097EE346D63}"
Public Const FOLDERID_PrintersFolder               As String = "{76FC4E2D-D6AD-4519-A663-37BD56068185}"
Public Const FOLDERID_SyncManagerFolder            As String = "{43668BF8-C14E-49B2-97C9-747784D784B7}"
Public Const FOLDERID_SyncSetupFolder              As String = "{0F214138-B1D3-4A90-BBA9-27CBC0C5389A}"
Public Const FOLDERID_ConflictFolder               As String = "{4BFEFB45-347D-4006-A5BE-AC0CB0567192}"
Public Const FOLDERID_SyncResultsFolder            As String = "{289A9A43-BE44-4057-A41B-587A76D7E7F9}"
Public Const FOLDERID_RecycleBinFolder             As String = "{B7534046-3ECB-4C18-BE4E-64CD4CB7D6AC}"
Public Const FOLDERID_ConnectionsFolder            As String = "{6F0CD92B-2E97-45D1-88FF-B0D186B8DEDD}"
Public Const FOLDERID_Fonts                        As String = "{FD228CB7-AE11-4AE3-864C-16F3910AB8FE}"
 
Public Const FOLDERID_Desktop                      As String = "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}"
 
Public Const FOLDERID_Startup                      As String = "{B97D20BB-F46A-4C97-BA10-5E3608430854}"
Public Const FOLDERID_Programs                     As String = "{A77F5D77-2E2B-44C3-A6A2-ABA601054A51}"
Public Const FOLDERID_StartMenu                    As String = "{625B53C3-AB48-4EC1-BA1F-A1EF4146FC19}"
Public Const FOLDERID_Recent                       As String = "{AE50C081-EBD2-438A-8655-8A092E34987A}"
Public Const FOLDERID_SendTo                       As String = "{8983036C-27C0-404B-8F08-102D10DCFD74}"
Public Const FOLDERID_Documents                    As String = "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}"
Public Const FOLDERID_Favorites                    As String = "{1777F761-68AD-4D8A-87BD-30B759FA33DD}"
Public Const FOLDERID_NetHood                      As String = "{C5ABBF53-E17F-4121-8900-86626FC2C973}"
Public Const FOLDERID_PrintHood                    As String = "{9274BD8D-CFD1-41C3-B35E-B13F55A758F4}"
Public Const FOLDERID_Templates                    As String = "{A63293E8-664E-48DB-A079-DF759E0509F7}"
Public Const FOLDERID_CommonStartup                As String = "{82A5EA35-D9CD-47C5-9629-E15D2F714E6E}"
Public Const FOLDERID_CommonPrograms               As String = "{0139D44E-6AFE-49F2-8690-3DAFCAE6FFB8}"
Public Const FOLDERID_CommonStartMenu              As String = "{A4115719-D62E-491D-AA7C-E74B8BE3B067}"
Public Const FOLDERID_PublicDesktop                As String = "{C4AA340D-F20F-4863-AFEF-F87EF2E6BA25}"
Public Const FOLDERID_ProgramData                  As String = "{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}"
Public Const FOLDERID_CommonTemplates              As String = "{B94237E7-57AC-4347-9151-B08C6C32D1F7}"
Public Const FOLDERID_PublicDocuments              As String = "{ED4824AF-DCE4-45A8-81E2-FC7965083634}"
Public Const FOLDERID_RoamingAppData               As String = "{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}"
Public Const FOLDERID_LocalAppData                 As String = "{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}"
Public Const FOLDERID_LocalAppDataLow              As String = "{A520A1A4-1780-4FF6-BD18-167343C5AF16}"
Public Const FOLDERID_InternetCache                As String = "{352481E8-33BE-4251-BA85-6007CAEDCF9D}"
Public Const FOLDERID_Cookies                      As String = "{2B0F765D-C0E9-4171-908E-08A611B84FF6}"
Public Const FOLDERID_History                      As String = "{D9DC8A3B-B784-432E-A781-5A1130A75963}"
Public Const FOLDERID_System                       As String = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}"
Public Const FOLDERID_SystemX86                    As String = "{D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}"
Public Const FOLDERID_Windows                      As String = "{F38BF404-1D43-42F2-9305-67DE0B28FC23}"
Public Const FOLDERID_Profile                      As String = "{5E6C858F-0E22-4760-9AFE-EA3317B67173}"
Public Const FOLDERID_Pictures                     As String = "{33E28130-4E1E-4676-835A-98395C3BC3BB}"
Public Const FOLDERID_ProgramFilesX86              As String = "{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}"
Public Const FOLDERID_ProgramFilesCommonX86        As String = "{DE974D24-D9C6-4D3E-BF91-F4455120B917}"
Public Const FOLDERID_ProgramFilesX64              As String = "{6D809377-6AF0-444b-8957-A3773F02200E}"
Public Const FOLDERID_ProgramFilesCommonX64        As String = "{6365D5A7-0F0D-45e5-87F6-0DA56B6A4F7D}"
Public Const FOLDERID_ProgramFiles                 As String = "{905E63B6-C1BF-494E-B29C-65B732D3D21A}"
Public Const FOLDERID_ProgramFilesCommon           As String = "{F7F1ED05-9F6D-47A2-AAAE-29D317C6F066}"
Public Const FOLDERID_UserProgramFiles             As String = "{5CD7AEE2-2219-4A67-B85D-6C9CE15660CB}"
Public Const FOLDERID_UserProgramFilesCommon       As String = "{BCBD3057-CA5C-4622-B42D-BC56DB0AE516}"
Public Const FOLDERID_AdminTools                   As String = "{724EF170-A42D-4FEF-9F26-B60E846FBA4F}"
Public Const FOLDERID_CommonAdminTools             As String = "{D0384E7D-BAC3-4797-8F14-CBA229B392B5}"
Public Const FOLDERID_Music                        As String = "{4BD8D571-6D19-48D3-BE97-422220080E43}"
Public Const FOLDERID_Videos                       As String = "{18989B1D-99B5-455B-841C-AB7C74E4DDFC}"
Public Const FOLDERID_Ringtones                    As String = "{C870044B-F49E-4126-A9C3-B52A1FF411E8}"
Public Const FOLDERID_PublicPictures               As String = "{B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5}"
Public Const FOLDERID_PublicMusic                  As String = "{3214FAB5-9757-4298-BB61-92A9DEAA44FF}"
Public Const FOLDERID_PublicVideos                 As String = "{2400183A-6185-49FB-A2D8-4A392A602BA3}"
Public Const FOLDERID_PublicRingtones              As String = "{E555AB60-153B-4D17-9F04-A5FE99FC15EC}"
Public Const FOLDERID_ResourceDir                  As String = "{8AD10C31-2ADB-4296-A8F7-E4701232C972}"
Public Const FOLDERID_LocalizedResourcesDir        As String = "{2A00375E-224C-49DE-B8D1-440DF7EF3DDC}"
Public Const FOLDERID_CommonOEMLinks               As String = "{C1BAE2D0-10DF-4334-BEDD-7AA20B227A9D}"
Public Const FOLDERID_CDBurning                    As String = "{9E52AB10-F80D-49DF-ACB8-4330F5687855}"
Public Const FOLDERID_UserProfiles                 As String = "{0762D272-C50A-4BB0-A382-697DCD729B80}"
Public Const FOLDERID_Playlists                    As String = "{DE92C1C7-837F-4F69-A3BB-86E631204A23}"
Public Const FOLDERID_SamplePlaylists              As String = "{15CA69B3-30EE-49C1-ACE1-6B5EC372AFB5}"
Public Const FOLDERID_SampleMusic                  As String = "{B250C668-F57D-4EE1-A63C-290EE7D1AA1F}"
Public Const FOLDERID_SamplePictures               As String = "{C4900540-2379-4C75-844B-64E6FAF8716B}"
Public Const FOLDERID_SampleVideos                 As String = "{859EAD94-2E85-48AD-A71A-0969CB56A6CD}"
Public Const FOLDERID_PhotoAlbums                  As String = "{69D2CF90-FC33-4FB7-9A0C-EBB0F0FCB43C}"
Public Const FOLDERID_Public                       As String = "{DFDF76A2-C82A-4D63-906A-5644AC457385}"
Public Const FOLDERID_ChangeRemovePrograms         As String = "{DF7266AC-9274-4867-8D55-3BD661DE872D}"
Public Const FOLDERID_AppUpdates                   As String = "{A305CE99-F527-492B-8B1A-7E76FA98D6E4}"
Public Const FOLDERID_AddNewPrograms               As String = "{DE61D971-5EBC-4F02-A3A9-6C82895E5C04}"
Public Const FOLDERID_Downloads                    As String = "{374DE290-123F-4565-9164-39C4925E467B}"
Public Const FOLDERID_PublicDownloads              As String = "{3D644C9B-1FB8-4F30-9B45-F670235F79C0}"
Public Const FOLDERID_SavedSearches                As String = "{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}"
Public Const FOLDERID_QuickLaunch                  As String = "{52A4F021-7B75-48A9-9F6B-4B87A210BC8F}"
Public Const FOLDERID_Contacts                     As String = "{56784854-C6CB-462B-8169-88E350ACB882}"
Public Const FOLDERID_SidebarParts                 As String = "{A75D362E-50FC-4FB7-AC2C-A8BEAA314493}"
Public Const FOLDERID_SidebarDefaultParts          As String = "{7B396E54-9EC5-4300-BE0A-2482EBAE1A26}"
'Public Const FOLDERID_TreeProperties              As String = "{5B3749AD-B49F-49C1-83EB-15370FBD4882}"   ' // Removed in Windows 7 SDK
Public Const FOLDERID_PublicGameTasks              As String = "{DEBF2536-E1A8-4C59-B6A2-414586476AEA}"
Public Const FOLDERID_GameTasks                    As String = "{054FAE61-4DD8-4787-80B6-090220C4B700}"
Public Const FOLDERID_SavedGames                   As String = "{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}"
Public Const FOLDERID_Games                        As String = "{CAC52C1A-B53D-4EDC-92D7-6B2E8AC19434}"
'Public Const FOLDERID_RecordedTV                  As String = "{BD85E001-112E-431E-983B-7B15AC09FFF1}"   ' // Removed in Windows 7 SDK
Public Const FOLDERID_SEARCH_MAPI                  As String = "{98EC0E18-2098-4D44-8644-66979315A281}"
Public Const FOLDERID_SEARCH_CSC                   As String = "{EE32E446-31CA-4ABA-814F-A5EBD2FD6D5E}"
Public Const FOLDERID_Links                        As String = "{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}"
Public Const FOLDERID_UsersFiles                   As String = "{F3CE0F7C-4901-4ACC-8648-D5D44B04EF8F}"
Public Const FOLDERID_UsersLibraries               As String = "{A302545D-DEFF-464B-ABE8-61C8648D939B}"
Public Const FOLDERID_SearchHome                   As String = "{190337D1-B8CA-4121-A639-6D472D16972A}"
Public Const FOLDERID_OriginalImages               As String = "{2C36C0AA-5812-4B87-BFD0-4CD0DFB19B39}"
Public Const FOLDERID_DocumentsLibrary             As String = "{7B0DB17D-9CD2-4A93-9733-46CC89022E7C}"
Public Const FOLDERID_MusicLibrary                 As String = "{2112AB0A-C86A-4FFE-A368-0DE96E47012E}"
Public Const FOLDERID_PicturesLibrary              As String = "{A990AE9F-A03B-4E80-94BC-9912D7504104}"
Public Const FOLDERID_VideosLibrary                As String = "{491E922F-5643-4AF4-A7EB-4E7A138D8174}"
Public Const FOLDERID_RecordedTVLibrary            As String = "{1A6FDBA2-F42D-4358-A798-B74D745926C5}"
Public Const FOLDERID_HomeGroup                    As String = "{52528A6B-B9E3-4ADD-B60D-588C2DBA842D}"
Public Const FOLDERID_DeviceMetadataStore          As String = "{5CE4A5E9-E4EB-479D-B89F-130C02886155}"
Public Const FOLDERID_Libraries                    As String = "{1B3EA5DC-B587-4786-B4EF-BD1DC332AEAE}"
Public Const FOLDERID_PublicLibraries              As String = "{48DAF80B-E6CF-4F4E-B800-0E69D84EE384}"
Public Const FOLDERID_UserPinned                   As String = "{9E3995AB-1F9C-4F13-B827-48B24B6C7174}"
Public Const FOLDERID_ImplicitAppShortcuts         As String = "{BCB5256F-79F6-4CEE-B725-DC34E402FD46}"
 
Private Type GUID
   Data1 As Long
   Data2 As Integer
   Data3 As Integer
   Data4(7) As Byte
End Type
 
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 Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
   Destination As Any, _
   Source As Any, _
   ByVal length As Long)
 
Private Declare Sub CoTaskMemFree Lib "ole32" ( _
   ByVal pv As Long)
 
Private Declare Function lstrlenW Lib "kernel32" ( _
   ByVal lpString As Long) As Long
 
 
Public Function GetSpecialFolder(sRfid) As String
 
   ' Quelle: www.dbwiki.net oder www.dbwiki.de
 
   Dim ppszPath As Long
   Dim gGuid    As GUID
 
   ' versuche, die Knownfolder-ID (GUID) in eine CLSID zu konvertieren
   If CLSIDFromString(StrPtr(sRfid), gGuid) = 0 Then
      'gGuid an Funktion SHGetKnownFolderPath übergeben,
      'die den Pfad als Zeiger zu einem Unicode-String zurückgibt
      If SHGetKnownFolderPath(gGuid, 0, 0, ppszPath) = 0 Then
         'wenn kein Fehler auftritt, Pfad, falls vorhanden, zurückgeben
         GetSpecialFolder = GetBstrFromWideStringPtr(ppszPath)
         'Speicher freigeben
         Call CoTaskMemFree(ppszPath)
      End If
   End If
End Function
 
Private Function GetBstrFromWideStringPtr(ByVal lpwString As Long) As String
 
   ' Quelle: www.dbwiki.net oder www.dbwiki.de
 
   Dim length As Long
 
   If lpwString Then
      'Länge des Strings ermitteln
      length = lstrlenW(lpwString)
      If length Then
         'Puffer für den String erzeugen,
         GetBstrFromWideStringPtr = Space$(length)
         'dem Rückgabewert der Funktion zuordnen
         CopyMemory ByVal StrPtr(GetBstrFromWideStringPtr), ByVal lpwString, _
                    length * 2
      End If
   End If
End Function

Aufruf

Den Ordner "Eigene Bilder" erhält man mit:

   ' Möglichkeit 1:
   ' Aufruf im Formular, GUID aus FOLDERID-Konstanten-Tabelle auslesen
   MsgBox GetSpecialFolder("{33E28130-4E1E-4676-835A-98395C3BC3BB}")
   ' Möglichkeit 2:
 
   ' FOLDERID-Konstante mit GUID in einem globalen Modul hinterlegen
   Public Const FOLDERID_Pictures = "{33E28130-4E1E-4676-835A-98395C3BC3BB}"
 
   ' Aufruf im Formular
   MsgBox GetSpecialFolder(FOLDERID_Pictures)


FOLDERID-Konstanten

Bekannte Ordner Known Folders Konstante GUID
Account Pictures FOLDERID_AccountPictures {008ca0b1-55b4-4c56-b8a8-4de4b299d3be}
Get Programs FOLDERID_AddNewPrograms {de61d971-5ebc-4f02-a3a9-6c82895e5c04}
Ordner Administrative Tools Administrative Tools FOLDERID_AdminTools {724EF170-A42D-4FEF-9F26-B60E846FBA4F}
Application Shortcuts FOLDERID_ApplicationShortcuts {A3918781-E5F2-4890-B3D9-A7E54332328C}
Applications FOLDERID_AppsFolder {1e87508d-89c2-42f0-8a7e-645a0f50ca58}
Installed Updates FOLDERID_AppUpdates {a305ce99-f527-492b-8b1a-7e76fa98d6e4}
Camera Roll FOLDERID_CameraRoll {AB5FB87B-7CE2-4F83-915D-550846C9537B}
Ordner Brennordner Temporary Burn Folder FOLDERID_CDBurning {9E52AB10-F80D-49DF-ACB8-4330F5687855}
Programs and Features FOLDERID_ChangeRemovePrograms {df7266ac-9274-4867-8d55-3bd661de872d}
Ordner Administrative Tools Administrative Tools FOLDERID_CommonAdminTools {D0384E7D-BAC3-4797-8F14-CBA229B392B5}
OEM Links FOLDERID_CommonOEMLinks {C1BAE2D0-10DF-4334-BEDD-7AA20B227A9D}
Ordner Startmenü\Programme Programs FOLDERID_CommonPrograms {0139D44E-6AFE-49F2-8690-3DAFCAE6FFB8}
Ordner Startmenü Start Menu FOLDERID_CommonStartMenu {A4115719-D62E-491D-AA7C-E74B8BE3B067}
Ordner Autostart Startup FOLDERID_CommonStartup {82A5EA35-D9CD-47C5-9629-E15D2F714E6E}
Ordner Templates Templates FOLDERID_CommonTemplates {B94237E7-57AC-4347-9151-B08C6C32D1F7}
Computer FOLDERID_ComputerFolder {0AC0837C-BBF8-452A-850D-79D08E667CA7}
Conflicts FOLDERID_ConflictFolder {4bfefb45-347d-4006-a5be-ac0cb0567192}
Network Connections FOLDERID_ConnectionsFolder {6F0CD92B-2E97-45D1-88FF-B0D186B8DEDD}
Ordner Kontakte Contacts FOLDERID_Contacts {56784854-C6CB-462b-8169-88E350ACB882}
Control Panel FOLDERID_ControlPanelFolder {82A74AEB-AEB4-465C-A014-D097EE346D63}
Ordner Cookies Cookies FOLDERID_Cookies {2B0F765D-C0E9-4171-908E-08A611B84FF6}
Ordner Desktop Desktop FOLDERID_Desktop {B4BFCC3A-DB2C-424C-B029-7FE99A87C641}
Ordner DeviceMetadataStore DeviceMetadataStore FOLDERID_DeviceMetadataStore {5CE4A5E9-E4EB-479D-B89F-130C02886155}
Ordner Eigene Dokumente Documents FOLDERID_Documents {FDD39AD0-238F-46AF-ADB4-6C85480369C7}
Ordner Eigene Dokumente Documents FOLDERID_DocumentsLibrary {7B0DB17D-9CD2-4A93-9733-46CC89022E7C}
Ordner Downloads Downloads FOLDERID_Downloads {374DE290-123F-4565-9164-39C4925E467B}
Ordner Favoriten Favorites FOLDERID_Favorites {1777F761-68AD-4D8A-87BD-30B759FA33DD}
Ordner Schriftarten Fonts FOLDERID_Fonts {FD228CB7-AE11-4AE3-864C-16F3910AB8FE}
Games FOLDERID_Games {CAC52C1A-B53D-4edc-92D7-6B2E8AC19434}
Ordner GameExplorer GameExplorer FOLDERID_GameTasks {054FAE61-4DD8-4787-80B6-090220C4B700}
Ordner History History FOLDERID_History {D9DC8A3B-B784-432E-A781-5A1130A75963}
Homegroup FOLDERID_HomeGroup {52528A6B-B9E3-4ADD-B60D-588C2DBA842D}
The user's username
(%USERNAME%)
FOLDERID_HomeGroupCurrentUser {9B74B6A3-0DFD-4f11-9E78-5F7800F2E772}
Ordner ImplicitAppShortcuts ImplicitAppShortcuts FOLDERID_ImplicitAppShortcuts {BCB5256F-79F6-4CEE-B725-DC34E402FD46}
Ordner Temporary Internet Files Temporary Internet Files FOLDERID_InternetCache {352481E8-33BE-4251-BA85-6007CAEDCF9D}
The Internet FOLDERID_InternetFolder {4D9F7874-4E0C-4904-967B-40B0D20C3E4B}
Ordner Libraries Libraries FOLDERID_Libraries {1B3EA5DC-B587-4786-B4EF-BD1DC332AEAE}
Ordner Links Links FOLDERID_Links {bfb9d5e0-c6a9-404c-b2b2-ae6db6af4968}
Ordner Local Local FOLDERID_LocalAppData {F1B32785-6FBA-4FCF-9D55-7B8E7F157091}
Ordner LocalLow LocalLow FOLDERID_LocalAppDataLow {A520A1A4-1780-4FF6-BD18-167343C5AF16}
None FOLDERID_LocalizedResourcesDir {2A00375E-224C-49DE-B8D1-440DF7EF3DDC}
Ordner Eigene Musik Music FOLDERID_Music {4BD8D571-6D19-48D3-BE97-422220080E43}
Ordner Eigene Musik Music FOLDERID_MusicLibrary {2112AB0A-C86A-4FFE-A368-0DE96E47012E}
Ordner Network Shortcuts Network Shortcuts FOLDERID_NetHood {C5ABBF53-E17F-4121-8900-86626FC2C973}
Network FOLDERID_NetworkFolder {D20BEEC4-5CA8-4905-AE3B-BF251EA09B53}
Original Images FOLDERID_OriginalImages {2C36C0AA-5812-4b87-BFD0-4CD0DFB19B39}
Slide Shows FOLDERID_PhotoAlbums {69D2CF90-FC33-4FB7-9A0C-EBB0F0FCB43C}
Ordner Eigene Bilder Pictures FOLDERID_PicturesLibrary {A990AE9F-A03B-4E80-94BC-9912D7504104}
Ordner Eigene Bilder Pictures FOLDERID_Pictures {33E28130-4E1E-4676-835A-98395C3BC3BB}
Playlists FOLDERID_Playlists {DE92C1C7-837F-4F69-A3BB-86E631204A23}
Printers FOLDERID_PrintersFolder {76FC4E2D-D6AD-4519-A663-37BD56068185}
Ordner Printer Shortcuts Printer Shortcuts FOLDERID_PrintHood {9274BD8D-CFD1-41C3-B35E-B13F55A758F4}
Ordner Benutzer The user's username
(%USERNAME%)
FOLDERID_Profile {5E6C858F-0E22-4760-9AFE-EA3317B67173}
Ordner ProgramData ProgramData FOLDERID_ProgramData {62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}
Ordner Programme (x86) Program Files FOLDERID_ProgramFiles {905e63b6-c1bf-494e-b29c-65b732d3d21a}
Ordner Programme Program Files FOLDERID_ProgramFilesX64 {6D809377-6AF0-444b-8957-A3773F02200E}
Ordner Programme (x86) Program Files FOLDERID_ProgramFilesX86 {7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}
Ordner Common Files Common Files FOLDERID_ProgramFilesCommon {F7F1ED05-9F6D-47A2-AAAE-29D317C6F066}
Common Files FOLDERID_ProgramFilesCommonX64 {6365D5A7-0F0D-45E5-87F6-0DA56B6A4F7D}
Ordner Common Files Common Files FOLDERID_ProgramFilesCommonX86 {DE974D24-D9C6-4D3E-BF91-F4455120B917}
Ordner Programs Programs FOLDERID_Programs {A77F5D77-2E2B-44C3-A6A2-ABA601054A51}
Ordner Public Public FOLDERID_Public {DFDF76A2-C82A-4D63-906A-5644AC457385}
Ordner Public\Desktop Public Desktop FOLDERID_PublicDesktop {C4AA340D-F20F-4863-AFEF-F87EF2E6BA25}
Ordner Public\Documents Public Documents FOLDERID_PublicDocuments {ED4824AF-DCE4-45A8-81E2-FC7965083634}
Ordner Public\Downloads Public Downloads FOLDERID_PublicDownloads {3D644C9B-1FB8-4f30-9B45-F670235F79C0}
Ordner GameExplorer GameExplorer FOLDERID_PublicGameTasks {DEBF2536-E1A8-4c59-B6A2-414586476AEA}
Ordner Libraries Libraries FOLDERID_PublicLibraries {48DAF80B-E6CF-4F4E-B800-0E69D84EE384}
Ordner Public\Music Public Music FOLDERID_PublicMusic {3214FAB5-9757-4298-BB61-92A9DEAA44FF}
Ordner Public\Pictures Public Pictures FOLDERID_PublicPictures {B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5}
Ordner Ringtones Ringtones FOLDERID_PublicRingtones {E555AB60-153B-4D17-9F04-A5FE99FC15EC}
Public Account Pictures FOLDERID_PublicUserTiles {0482af6c-08f1-4c34-8c90-e17ec98b1e17}
Ordner Public\Videos Public Videos FOLDERID_PublicVideos {2400183A-6185-49FB-A2D8-4A392A602BA3}
Ordner Internet Explorer\Quick Launch Quick Launch FOLDERID_QuickLaunch {52a4f021-7b75-48a9-9f6b-4b87a210bc8f}
Ordner Recent Recent Items FOLDERID_Recent {AE50C081-EBD2-438A-8655-8A092E34987A}
Ordner RecordedTV.library-ms Recorded TV FOLDERID_RecordedTVLibrary {1A6FDBA2-F42D-4358-A798-B74D745926C5}
Recycle Bin FOLDERID_RecycleBinFolder {B7534046-3ECB-4C18-BE4E-64CD4CB7D6AC}
Ordner resources Resources FOLDERID_ResourceDir {8AD10C31-2ADB-4296-A8F7-E4701232C972}
Ordner Ringtones Ringtones FOLDERID_Ringtones {C870044B-F49E-4126-A9C3-B52A1FF411E8}
Ordner Roaming Roaming FOLDERID_RoamingAppData {3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}
RoamedTileImages FOLDERID_RoamedTileImages {AAA8D5A5-F1D6-4259-BAA8-78E7EF60835E}
RoamingTiles FOLDERID_RoamingTiles {00BCFC5A-ED94-4e48-96A1-3F6217F21990}
Ordner Sample Music Sample Music FOLDERID_SampleMusic {B250C668-F57D-4EE1-A63C-290EE7D1AA1F}
Ordner Sample Pictures Sample Pictures FOLDERID_SamplePictures {C4900540-2379-4C75-844B-64E6FAF8716B}
Sample Playlists FOLDERID_SamplePlaylists {15CA69B3-30EE-49C1-ACE1-6B5EC372AFB5}
Ordner Sample Videos Sample Videos FOLDERID_SampleVideos {859EAD94-2E85-48AD-A71A-0969CB56A6CD}
Ordner Saved Games Saved Games FOLDERID_SavedGames {4C5C32FF-BB9D-43b0-B5B4-2D72E54EAAA4}
Saved Pictures FOLDERID_SavedPictures {3B193882-D3AD-4eab-965A-69829D1FB59F}
Saved Pictures Library FOLDERID_SavedPicturesLibrary {E25B5812-BE88-4bd9-94B0-29233477B6C3}
Ordner Searches Searches FOLDERID_SavedSearches {7d1d3a04-debb-4115-95cf-2f29da2920da}
Screenshots FOLDERID_Screenshots {b7bede81-df94-4682-a7d8-57a52620b86f}
Offline Files FOLDERID_SEARCH_CSC {ee32e446-31ca-4aba-814f-a5ebd2fd6d5e}
History FOLDERID_SearchHistory {0D4C3DB6-03A3-462F-A0E6-08924C41B5D4}
Search Results FOLDERID_SearchHome {190337d1-b8ca-4121-a639-6d472d16972a}
Microsoft Office Outlook FOLDERID_SEARCH_MAPI {98ec0e18-2098-4d44-8644-66979315a281}
Templates FOLDERID_SearchTemplates {7E636BFE-DFA9-4D5E-B456-D7B39851D8A9}
Ordner SendTo SendTo FOLDERID_SendTo {8983036C-27C0-404B-8F08-102D10DCFD74}
Ordner Gadgets Gadgets FOLDERID_SidebarDefaultParts {7B396E54-9EC5-4300-BE0A-2482EBAE1A26}
Ordner Gadgets Gadgets FOLDERID_SidebarParts {A75D362E-50FC-4fb7-AC2C-A8BEAA314493}
OneDrive FOLDERID_SkyDrive {A52BBA46-E9E1-435f-B3D9-28DAA648C0F6}
Camera Roll FOLDERID_SkyDriveCameraRoll {767E6811-49CB-4273-87C2-20F355E1085B}
Documents FOLDERID_SkyDriveDocuments {24D89E24-2F19-4534-9DDE-6A6671FBB8FE}
Pictures FOLDERID_SkyDrivePictures {339719B5-8C47-4894-94C2-D8F77ADD44A6}
Ordner Start Menu Start Menu FOLDERID_StartMenu {625B53C3-AB48-4EC1-BA1F-A1EF4146FC19}
Ordner Autostart Startup FOLDERID_Startup {B97D20BB-F46A-4C97-BA10-5E3608430854}
Sync Center FOLDERID_SyncManagerFolder {43668BF8-C14E-49B2-97C9-747784D784B7}
Sync Results FOLDERID_SyncResultsFolder {289a9a43-be44-4057-a41b-587a76d7e7f9}
Sync Setup FOLDERID_SyncSetupFolder {0F214138-B1D3-4a90-BBA9-27CBC0C5389A}
Ordner system32 System32 FOLDERID_System {1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}
Ordner SysWOW64 System32 FOLDERID_SystemX86 {D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}
Ordner Templates Templates FOLDERID_Templates {A63293E8-664E-48DB-A079-DF759E0509F7}
Ordner User Pinned User Pinned FOLDERID_UserPinned {9E3995AB-1F9C-4F13-B827-48B24B6C7174}
Ordner Benutzer Users FOLDERID_UserProfiles {0762D272-C50A-4BB0-A382-697DCD729B80}
Ordner Programs Programs FOLDERID_UserProgramFiles {5CD7AEE2-2219-4A67-B85D-6C9CE15660CB}
Ordner Programs\Common Programs FOLDERID_UserProgramFilesCommon {BCBD3057-CA5C-4622-B42D-BC56DB0AE516}
The user's full name (for instance, Jean Philippe Bagel) entered when the user account was created. FOLDERID_UsersFiles {f3ce0f7c-

4901-4acc-8648-d5d44b04ef8f}

Libraries FOLDERID_UsersLibraries {A302545D-DEFF-464b-ABE8-61C8648D939B}
Ordner Eigene Videos Videos FOLDERID_Videos {18989B1D-99B5-455B-841C-AB7C74E4DDFC}
Ordner Eigene Videos Videos FOLDERID_VideosLibrary {491E922F-5643-4AF4-A7EB-4E7A138D8174}
Ordner Windows Windows FOLDERID_Windows {F38BF404-1D43-42F2-9305-67DE0B28FC23}


Wiki hinweis.png Anmerkung: Die Ordner "Programme" bzw. "Programme (X86)" werden je nach Windows- und Accessversion (32 bit oder 64 bit) angezeigt, oder nicht angezeigt (siehe unter "Remarks" auf der Microsoft-Seite "KNOWNFOLDERID")


Wikilinks


Weblinks