set last used for getting directory

S

smokiibear

1) Using the following code, I would like the file browser open up to the
last used directory.

2) I borrowed this code from previous posts, but I do not clearly
understand it. Could anyone direct me to a source where I could study this
more in depth?

Thank You.

Smokii

Option Explicit

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


'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String)
As Long


Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Function GetDirectory(Optional msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer


' Root folder = Desktop
bInfo.pidlRoot = 0&


' Title in the dialog
If IsMissing(msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = msg
End If


' Type of directory to return
bInfo.ulFlags = &H1


' Display the dialog
x = SHBrowseForFolder(bInfo)


' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
call Cancel
End If
End Function
 
N

Nate Oliver

Hello,

Ssee the following at Ivan F Moala's site:

http://www.xcelfiles.com/Shell32_00.html

Change Sub TesterIII() to pass CurDir. I.e.,

'-------------------------------
Sub TesterIII()
'// Using String
'// This will not only limit the User to a specific Folder
Dim strFolder As String

strFolder = BrowseForFolderShell(, , , CurDir)

If strFolder = vbNullString Then
MsgBox "You cancelled"
Else
MsgBox strFolder
End If

End Sub
'-------------------------------

Regards,
Nate Oliver
 
S

smokiibear

strFolder = BrowseForFolderShell(, , , CurDir)

this line yields an error: "Sub or Function not defined"

Did I miss something?

Smokii
 
N

Nate Oliver

Did I miss something?

Yes, the function that Sub TesterIII() is calling, that being
BrowseForFolderShell().

Try the following:

'-------------------------------------------------------------------------
Public Function BrowseForFolderShell( _
Optional Hwnd As Long = 0, _
Optional sTitle As String = vbNullString, _
Optional BIF_Options As Long = &H20, _
Optional vRootFolder As Variant) As String

Dim objShell As Object
Dim objFolder As Object
Dim strFolderFullPath As String

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell. _
BrowseForFolder(Hwnd, sTitle, BIF_Options, vRootFolder)

If (Not objFolder Is Nothing) Then
'// NB: If SpecFolder= 0 = Desktop then ....
On Error Resume Next
If IsError(objFolder.Items.Item.Path) Then _
strFolderFullPath = CStr(objFolder): GoTo GotIt
On Error GoTo 0
'// Is it the Root Dir?...if so change
If Len(objFolder.Items.Item.Path) > 3 Then
strFolderFullPath = objFolder.Items.Item.Path & _
Application.PathSeparator
Else
strFolderFullPath = objFolder.Items.Item.Path
End If
Else
'// User cancelled
GoTo XitProperly
End If

GotIt:
BrowseForFolderShell = strFolderFullPath

XitProperly:
Set objFolder = Nothing
Set objShell = Nothing

End Function

Sub TesterIII()
'// Using String
'// This will not only limit the User to a specific Folder
Dim strFolder As String

Call BrowseForFolderShell(, , , CurDir)

If strFolder = vbNullString Then
MsgBox "You cancelled"
Else
MsgBox strFolder
End If

End Sub
'-------------------------------------------------------------------------

Regards,
Nate Oliver
 
N

Nate Oliver

Yikes, use the function I last posted, do not use the routing 'TesterIII()'.

Use:

Sub TesterIII()
'// Using String
'// This will not only limit the User to a specific Folder
Dim strFolder As String

Let strFolder = BrowseForFolderShell(, , , CurDir)

If strFolder = vbNullString Then
MsgBox "You cancelled"
Else
MsgBox strFolder
End If

End Sub
'----------------

You want to pass the string to a variable. Sorry about the confusion.

Regards,
Nate Oliver
 
S

smokiibear

it works...but it does not allow me to climb back up the directory from
the last used folder.

Smokii
 
S

smokiibear

I am using the following code below to select a folder, but the dialog
always appears in the upper right of my screen. How do I position this
dialog closer to the center of the screen? or, in parallel with one of
my recent posts, how could I have the dialog appear where it was last
moved to?

I would also like the browser to open up to the last chosen directory.

'--------------------------------------------------------------
Option Explicit

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


'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As
String) As Long


Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Function GetDirectory(Optional msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer


' Root folder = Desktop
bInfo.pidlRoot = 0&


' Title in the dialog
If IsMissing(msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = msg
End If


' Type of directory to return
bInfo.ulFlags = &H1


' Display the dialog
x = SHBrowseForFolder(bInfo)


' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
call Cancel
End If
End Function
'--------------------------------------------------------------


Thanks for your help.

Smokii
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top