ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   set last used for getting directory (https://www.excelbanter.com/excel-programming/318984-set-last-used-getting-directory.html)

smokiibear

set last used for getting directory
 
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

Nate Oliver[_3_]

set last used for getting directory
 
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


smokiibear

set last used for getting directory
 
strFolder = BrowseForFolderShell(, , , CurDir)

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

Did I miss something?

Smokii




Nate Oliver[_3_]

set last used for getting directory
 
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

Nate Oliver[_3_]

set last used for getting directory
 
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

smokiibear

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

Smokii

"?B?TmF0ZSBPbGl2ZXI=?="
wrote in :

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



smokiibear

How to postion folder browser
 
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

Nate Oliver[_3_]

set last used for getting directory
 
No, it doesn't:

http://msdn.microsoft.com/library/en...eforfolder.asp

See the description of the vRootFolder parameter.

Regards,
Nathan Oliver

"smokiibear" wrote:

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


smokiibear

How to postion folder browser
 
opps... i ment for this to be a new post...but just just as well. Sorry

smokii



All times are GMT +1. The time now is 05:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com