View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Rech Jim Rech is offline
external usenet poster
 
Posts: 2,718
Default Folder picker default - second attempt...

I didn't have any luck either trying to get the dialog to open at My
Computer. If no one comes up with an answer you might want to consider a
different approach. The following uses a built-in Windows function to
create an interface for picking a folder. The Windows' SHBrowseForFolder
function actually provides many more options than this example shows, but it
may be all you need. For a more complete example look at the
BrowseForFolder demo I uploaded to Stephen Bullen's site
(http://www.bmsltd.co.uk/MVP/Default.htm).

--
Jim Rech
Excel MVP

Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

'Main Browse for directory function
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
'Gets path from pidl
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String)
As Long

Sub Demo()
Dim RetStr As String
RetStr = GetDirectory("Choose path")
If RetStr < "" Then MsgBox RetStr
End Sub

Function GetDirectory(Optional Msg) As String
Dim bInfo As BROWSEINFO
Dim Path As String
Dim ItemIdentifierListAddress As Long
Dim Success As Boolean

bInfo.pidlRoot = 0 'Root folder = Desktop

If Not IsMissing(Msg) Then bInfo.lpszTitle = Msg
'Display the dialog
ItemIdentifierListAddress = SHBrowseForFolder(bInfo)
'Get path string from ID
GetDirectory = GetPathFromID(ItemIdentifierListAddress)
End Function

'Converts a PIDL to a string
Function GetPathFromID(ID As Long) As String
Dim Result As Boolean, Path As String * 255
Result = SHGetPathFromIDList(ID, Path)
If Result Then
GetPathFromID = Left(Path, InStr(Path, Chr$(0)) - 1)
Else
GetPathFromID = ""
End If
End Function