Skip to main content link. Accesskey S
  • Help
  • HCL Logo
  • HCL Notes and Domino Application Development wiki
  • THIS WIKI IS READ-ONLY. Individual names altered for privacy purposes.
  • HCL Forums and Blogs
  • Home
  • Product Documentation
  • Community Articles
  • Learning Center
  • API Documentation
Search
Community Articles > Programming > HowTo: Browse for folders
  • Share Show Menu▼
  • Subscribe Show Menu▼

Recent articles by this author

Traversing XML result of AJAX request with JQuery

When working with JQuery and Ajax, there is simply no simple good instructions on how to traverse the complete tree of an XML result of an Ajax call. This is how You do it.

A shortcut for get the number of rows used in Excelfile/Worksheet

When working with Excelfiles it is very common to know how many rows there is to process in the file so that You can inform the user in a progressbar or something. I've seen a numerous examples of simple looping through each row until the ActiveCell is a nullvalue (empty string). This is not very ...

How to create and send an email message containing an HTML body using an agent

To create an email that contains a HTML body is a quite simple process, but there is a lot to think about because of the variety of emailclients and their specific behaviour in how they implement the HTMLCSS specifications. I will here try to guide You through the complete steps needed to ...

Using XML response from Ajax call in Lotus Domino Web applications

There is a lot to say when it comes to AJAX! If You have read the article "Integrating Ajax into traditional IBM Lotus Domino Web applications" (http:www-10.lotus.comlddddwiki.nsfdxIntegratingAjaxintotraditionalIBMLotusDominoWebapplications) You will know how to use the Ajax response ...

HowTo: Create Bullet List in Word Document

I'm continuing the discussion on how to create a word document. Now it's time to understand how to format text in Word; This time I'll show how to write Bullet Lists into a word document. First, read up on how to create a word document here: ...
Community articleHowTo: Browse for folders
Added by ~Richard Zekboosilyettu | Edited by ~Richard Zekboosilyettu on June 29, 2010 | Version 7
  • Actions Show Menu▼
expanded Abstract
collapsed Abstract
In LotusScript You have the option to open files with the OpenFileDialog (or SaveFileDialog) or open databases with OpenDatabase methods found in the NotesUIWorkspace class. But what if You are only interested in getting user input for a FOLDER and not the full path to a file? Then You have to use Windows API. Here is how You do it.
Tags: win32 api, windows api

In LotusScript You have the option to open files with the OpenFileDialog (or SaveFileDialog) or open databases with OpenDatabase methods found in the NotesUIWorkspace class. But what if You are only interested in getting user input for a FOLDER and not the full path to a file? Then You have to use Windows API. Here is how You do it.
 
Obviosly (since this is Win32 API) it will only work on Windows. I have only tested it on Windows XP, but as far as I know - it should run on Windows 7 without problems. Tell me if I'm wrong!
 
I provide the code as is and there is a lot of comments that should explain what it's doing:
 

Option Public
Option Explicit
%REM
The Browse Dialog implementation is made up of three C Structures (BROWSEINFO, ITEMIDLIST and SHITEMID), 
and three APIs (SHBrowseForFolder, SHGetPathFromIDList and SHGetSpecialFolderLocation.)
Objects in the shell’s namespace are assigned item identifiers and item identifier lists. 
An item identifier uniquely identifies an item within its parent folder. An item identifier list 
uniquely identifies an item within the shell’s namespace by tracing a path to the item from the desktop.
An item identifier is defined by the variable-length SHITEMID structure. The first two bytes of this structure specify its 
size, and the format of the remaining bytes depends on the parent folder, or more precisely, on the software that 
implements the parent folder’s IShellFolder interface. Except for the first two bytes, item identifiers are not strictly 
defined, and applications should make no assumptions about their format. 
%ENDREM 
 
Public Const ERROR_SUCCESS = 0
'Windows desktop virtual folder at the root of the name space
Public Const CSIDL_DESKTOP = &H0
'File system directory that contains the user's program groups (which are also file system directories)
Public Const CSIDL_PROGRAMS = &H2
'Control Panel - virtual folder containing icons for the control panel applications 
Public Const CSIDL_CONTROLS = &H3
'Printers folder - virtual folder containing installed printers.
Public Const CSIDL_PRINTERS = &H4
'File system directory that serves as a common repository for documents (Documents folder)
Public Const CSIDL_PERSONAL = &H5
'File system directory that contains the user's Favourite Internet Explorer URLs 
Public Const CSIDL_FAVORITES = &H6
'File system directory that corresponds to the user's Startup program group  
Public Const CSIDL_STARTUP = &H7
'File system directory that contains the user's most recently used documents (Recent folder)
Public Const CSIDL_RECENT = &H8
'File system directory that contains Send To menu items
Public Const CSIDL_SENDTO = &H9
'Recycle bin file system directory containing file objects in the user's recycle bin. The location of 
'this directory is not in the registry; it is marked with the hidden and system attributes to prevent the
'user from moving or deleting it.
Public Const CSIDL_BITBUCKET = &HA
 
'File system directory containing Start menu items
Public Const CSIDL_STARTMENU = &HB
'File system directory used to physically store file objects on the desktop (not to be confused with the desktop folder itself). 
Public Const CSIDL_DESKTOPDIRECTORY = &H10
'My Computer - virtual folder containing everything on the local computer: storage devices, printers, and Control Panel. The folder may also contain mapped network drives.
Public Const CSIDL_DRIVES = &H11
'Network Neighborhood - virtual folder representing the top level of the network hierarchy
Public Const CSIDL_NETWORK = &H12
'File system directory containing objects that appear in the network neighborhood
Public Const CSIDL_NETHOOD = &H13
'Virtual folder containing fonts
Public Const CSIDL_FONTS = &H14
'File system directory that serves as a common repository for document templates (ShellNew folder.)
Public Const CSIDL_TEMPLATES = &H15
'The BROWSEINFO.ulFlags member specifies the type of folders to be listed in the dialog box as well 
'as other options. This member can include zero or more of the following values:
'Only returns file system directories. If the user selects folders that are not part of the file system, the OK button is grayed.
Public Const BIF_RETURNONLYFSDIRS = &H1
'Does not include network folders below the domain level in the tree view control. For starting the Find Computer. 
Public Const BIF_DONTGOBELOWDOMAIN = &H2
'Includes a status area in the dialog box. The callback function can set the status text by sending messages to the dialog box.
Public Const BIF_STATUSTEXT = &H4
'Only returns file system ancestors. If the user selects anything other than a file system ancestor, the OK button is grayed.
Public Const BIF_RETURNFSANCESTORS = &H8
'Only returns computers. If the user selects anything other than a computer, the OK button is grayed.
Public Const BIF_BROWSEFORCOMPUTER = &H1000
'Only returns (network) printers. If the user selects anything other than a printer, the OK button is grayed.
Public Const BIF_BROWSEFORPRINTER = &H2000
Public Type SHITEMID
 cb As Long     'Size of the ID (including cb itself)
 abID As Byte   'The item ID (variable length)
End Type
'The ITEMIDLIST structure defines an element in an item identifier list (the only member of this structure 
'is an SHITEMID structure). An item identifier list consists of one or more consecutive ITEMIDLIST structures packed on 
'byte boundaries, followed by a 16-bit zero value. An application can walk a list of item identifiers by examining 
'the size specified in each SHITEMID structure and stopping when it finds a size of zero. A pointer to an item identifier 
'list, is called a PIDL (pronounced piddle.) Note however that it is unnecessary to use the ITEMIDLIST structure; since the
'pidl is a long, it can be passed and referenced as such when implementing the APIs. The ITEMIDLIST discussion is provided 
'for completeness.
Public Type ITEMIDLIST
 mkid As SHITEMID
End Type
'The BROWSEINFO structure contains parameters for the SHBrowseForFolder function and receives information about the folder selected by the user. 
Public Type BROWSEINFO
'Handle of the owner window for the dialog box.      
hOwner As Long
 
'Pointer to an item identifier list (an ITEMIDLIST structure) specifying the location of the "root" folder to browse from. Only the 
'specified folder and its subfolders appear in the dialog box. This member can be NULL, and in that case, the namespace root (the desktop folder) is used.      
pidlRoot As Long
 
'Pointer to a buffer that receives the display name of the folder selected by the user. The size of this buffer is assumed to be MAX_PATH bytes.
pszDisplayName As String
 
'Pointer to a null-terminated string that is displayed above the tree view control in the 
'dialog box.This string can be used to specify instructions to the user.
lpszTitle As String
 
'Value specifying the types of folders to be listed in the dialog box as well as other options. 
'This member can include zero or more of the following values below.  
ulFlags As Long
 
'Address an application-defined function that the dialog box calls when events occur. For more information, 
'see the description of the BrowseCallbackProc function. This member can be NULL. (note: VB4 does not support 
'callbacks, therefore this member is ignored.)
lpfn As Long
 
'Application-defined value that the dialog box passes to the callback function (if one is specified).
lParam As Long
 
'Variable that receives the image associated with the selected folder. The image is specified as an index to the system image list.
iImage As Long
End Type
'SHGetPathFromIDList converts an item identifier list to a file system path. It returns 
'TRUE if successful or FALSE if an error occurs, for example, if the location specified by the pidl 
'parameter is not part of the file system.
Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" (Byval pidl As Long, Byval pszPath As String) As Long
'SHGetSpecialFolderLocation retrieves the location of a special (system) folder. It returns ERROR_SUCCESS 
'if successful or an OLE-defined error result otherwise.
Declare Function SHGetSpecialFolderLocation Lib "shell32"(Byval hwndOwner As Long, Byval nFolder As Long, pidl As ITEMIDLIST) As Long
'The SHGetSpecialFolderLocation "nFolder" param holds the value specifying the folder to retrieve the 
'location of. This parameter can be one of the following values. 
'Most folderlocations are stored in HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerShell Folders.
'A call to the SHBrowseForFolder API displays a dialog box that enables the user to select a shell folder. 
'The call returns a pointer to an item identifier list (PIDL) that specifies the location of the selected folder relative 
'to the root of the name space. If the user chooses the Cancel button in the dialog box, the return value is NULL.
Declare Function SHBrowseForFolder Lib "shell32"  Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long 
Declare Sub CoTaskMemFree Lib "ole32" (Byval pv As Long)
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetWindow Lib "user32" (Byval hwnd As Long, Byval wCmd As Long) As Long

 
When all declarations are in place... Here is the code to use the API:
 
 
Sub Initialize
 
 Dim hWnd As Long
 Dim hWndChild As Long
 Dim pos As Long
 Dim bi As BROWSEINFO
 Dim pidl As Long
 Dim path As String
 Const MAX_PATH = 260
 
 hWnd = GetDesktopWindow()
 hWndChild = GetWindow(hWnd,5)
 
     'hwnd of the window that receives messages 
     'from the call. Can be your application 
     'or the handle from GetDesktopWindow()      
 bi.hOwner = hWndChild
 
     'pointer to the item identifier list specifying
     'the location of the "root" folder to browse from.
     'If NULL, the desktop folder is used.
 bi.pidlRoot = 0&
 
     'message to be displayed in the Browse dialog
 bi.lpszTitle = "Select your Windows\System\ directory"
 
     'the type of folder to return.
 bi.ulFlags = BIF_RETURNONLYFSDIRS
 
  'show the Browse Dialog
 pidl = SHBrowseForFolder(bi)
 
  'the dialog has closed, so parse & display the
  'user's returned folder selection contained in pidl
 path = Space$(MAX_PATH)
 
 If SHGetPathFromIDList(Byval pidl, Byval path) Then
  pos = Instr(path, Chr$(0))
  'Label1.Caption = Left(path, pos - 1)
  Msgbox Left$(path,pos-1) ' Here You are getting the final result...
 End If
 
 Call CoTaskMemFree(pidl)
 
End Sub 

There is also a simpler, non-API way to do this:
 
Const BIF_NEWDIALOGSTYLE = &H00000040
Const BIF_NONEWFOLDERBUTTON = &H00000200

Dim objShell As Variant

Dim objFolder As Variant
Dim objFolderItem As Variant

Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.BrowseForFolder(0, "Please select a folder", BIF_NONEWFOLDERBUTTON + BIF_NEWDIALOGSTYLE, "C:\")
If Not (objFolder Is Nothing) Then
Set objFolderItem = objFolder.Self
Msgbox objFolderItem.Path 
End If

 


  • Actions Show Menu▼


expanded Attachments (0)
collapsed Attachments (0)
Edit the article to add or modify attachments.
expanded Versions (7)
collapsed Versions (7)
Version Comparison     
VersionDateChanged by              Summary of changes
This version (7)Jun 29, 2010, 8:16:56 AM~Richard Zekboosilyettu  
6Jun 29, 2010, 8:14:02 AM~Vanessa Preponelyynds  
5Jun 29, 2010, 8:11:45 AM~Vanessa Preponelyynds  
4Jun 17, 2010, 8:52:40 PM~Vanessa Preponelyynds  
3Jun 15, 2010, 6:42:46 AM~Vanessa Preponelyynds  
2Jun 15, 2010, 6:34:45 AM~Vanessa Preponelyynds  
1Jun 15, 2010, 6:32:06 AM~Vanessa Preponelyynds  
expanded Comments (0)
collapsed Comments (0)
Copy and paste this wiki markup to link to this article from another article in this wiki.
Go ElsewhereStay ConnectedAbout
  • HCL Software
  • HCL Digital Solutions community
  • HCL Software support
  • BlogsDigital Solutions blog
  • Community LinkHCL Software forums and blogs
  • About HCL
  • Privacy
  • Accessibility