Visual Basic Browse/ PIDL / CSIDL Routines
Pure VB: Using the Shell Application Object to Retrieve Windows Shell Folders
     
Posted:   Tuesday April 15, 2008
Updated:   Monday December 26, 2011
     
Applies to:   VB5, VB6
Developed with:   VB6, Windows Vista
OS restrictions:   None
Author:   VBnet - Larry Serflaten
     
Related:   BROWSE:
SHBrowseForFolder: Browse Folders Dialog Overview
SHBrowseForFolder: Browse for Folders Dialog
SHBrowseForFolder: Browse for Folders Callback Overview
SHBrowseForFolder: Pre-selecting Folders using a Browse Callback
SHBrowseForFolder: Browse for Folders New UI Features

CSIDL / Folders:
SHGetFolderPath: Overview of Shell and ComCtrl Versions, CSIDL Values
SHGetFolderPath: Retrieve Windows Shell Folders (Best Practice)

SHGetSpecialFolderLocation: Retrieve Windows Shell Folder
Pure VB: Using the Shell Application Object to Retrieve Windows Shell Folders

       
 Prerequisites
None.

 

Other pages have shown how to use various APIs to get file system paths to special folders (see Related above). Here's a pure-VB method provided by Larry Serflaten to do the same using built-in functionality of the Shell Application Object. Like the SHGetSpecialFolderLocation and SHGetFolderPath methods this code allows identification of special folders that may be unique to the machine the code is run on.  An additional benefit is the built-in Open method of the Shell Application Object that can be invoked to open the physical file location specified by the special folder.

 BAS Module Code
None.

 Form Code
To a project form add a command button (Command1), a text box (Text1) and a Combo (Combo1) set to style 2. Add the following to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private SA As Object    'The Shell Application object


Private Sub Form_Load()

  'init controls
   Text1.Text = "Select a Special Folder from the list below."
   Command1.Caption = "Open"
   Command1.Enabled = False
   
  'obtain Shell object and fill list
   Set SA = CreateObject("Shell.Application")
   FillCombo
   
End Sub


Private Sub Command1_Click()

   SA.Open Combo1.ItemData(Combo1.ListIndex)
   
End Sub


Private Sub Combo1_Click()

   Dim ID As Variant  'use Variant type in call to Namespace
   
  'show folder path
   ID = Combo1.ItemData(Combo1.ListIndex)
   Text1.Text = SA.Namespace(ID).Self.Path
   Command1.Enabled = True
   
End Sub


Private Sub FillCombo()

   Dim idx As Variant 'use Variant type in call to Namespace
   Dim folder As Object

  'fill combo box with displayed names
   For idx = 0 To 100
   
      Set folder = SA.Namespace(idx)
      
      If Not folder Is Nothing Then
     
         Combo1.AddItem Right$("   " & idx, 3) & "   " & SA.Namespace(idx).Title
         Combo1.ItemData(Combo1.NewIndex) = idx
         Set folder = Nothing
       
      End If
   Next
   
End Sub
 Comments
Run and select an item from the combo.  If the selected item represents a physical folder, and you system supports the call, the folder path will be displayed in the textbox.  Remember that virtual folders never return a physical path.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter