-
Select folder to save into
I want to use the common dialog box to select a folder which the user wants to save a load of files in.
When the user click a folder i want that directory path as a result.
When i do it using .ShowSave it will want to save a file, not select a folder to save to later.
I hope you all understand
ILMV
-
Re: Select folder to save into
Browse for Folder
VB Code:
Option Explicit
Private Const BIF_RETURNONLYFSDIRS = &H1
Private Const BIF_DONTGOBELOWDOMAIN = &H2
Private Const BIF_STATUSTEXT = &H4
Private Const BIF_RETURNFSANCESTORS = &H8
Private Const BIF_EDITBOX = &H10
Private Const BIF_VALIDATE = &H20
Private Const BIF_BROWSEFORCOMPUTER = &H1000
Private Const BIF_BROWSEFORPRINTER = &H2000
Private Const BIF_BROWSEINCLUDEFILES = &H4000
Private Const MAX_PATH = 260
Private Type T_BROWSEINFO
HwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHBrowseForFolder Lib "shell32" _
(lpbi As T_BROWSEINFO) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" _
(ByVal pidList As Long, _
ByVal lpBuffer As String) As Long
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" _
(ByVal lpString1 As String, ByVal _
lpString2 As String) As Long
Public Function BrowseFolder(ByVal HwndOwner As Long, ByRef Titre As String) As String
Dim lpIDList As Long
Dim sBuffer As String
Dim BrowseInfo As T_BROWSEINFO
BrowseFolder = ""
With BrowseInfo
.HwndOwner = HwndOwner
.lpszTitle = lstrcat(Titre, "")
.ulFlags = BIF_RETURNONLYFSDIRS
End With
lpIDList = SHBrowseForFolder(BrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
BrowseFolder = sBuffer
End If
End Function
-
Re: Select folder to save into
-
Re: Select folder to save into
Hack i am trying your method. How do i call t, i havnt got a clue.
Obv Call BrowseFolder, but whatare th parametres?
EDIT: Sorry, found out, however what fdoes the first parametre do?
-
Re: Select folder to save into
Just use the hWnd of the current form:
VB Code:
Private Sub Form_Load()
Dim str As String
str = BrowseFolder(Me.hWnd, "D:\Temp")
MsgBox str
End Sub
-
Re: Select folder to save into
Back again, i have another problem, i have used hack's method, but how do create a new folder, usually there is a button sayin "Creat Ne wFolder"
any ideas
ILMV
-
Re: Select folder to save into
Create a routine, separate from the Browse routine, that creates the folder for you. Put up a textbox and have the user enter the name they want, and then use MkDir to create it.
After that is done it will show up when you Browse.
-
Re: Select folder to save into
-
Re: Select folder to save into
Quote:
Originally Posted by Hack
Browse for Folder
VB Code:
Option Explicit
Private Const BIF_RETURNONLYFSDIRS = &H1
Private Const BIF_DONTGOBELOWDOMAIN = &H2
Private Const BIF_STATUSTEXT = &H4
Private Const BIF_RETURNFSANCESTORS = &H8
Private Const BIF_EDITBOX = &H10
Private Const BIF_VALIDATE = &H20
Private Const BIF_BROWSEFORCOMPUTER = &H1000
Private Const BIF_BROWSEFORPRINTER = &H2000
Private Const BIF_BROWSEINCLUDEFILES = &H4000
Private Const MAX_PATH = 260
Private Type T_BROWSEINFO
HwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHBrowseForFolder Lib "shell32" _
(lpbi As T_BROWSEINFO) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" _
(ByVal pidList As Long, _
ByVal lpBuffer As String) As Long
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" _
(ByVal lpString1 As String, ByVal _
lpString2 As String) As Long
Public Function BrowseFolder(ByVal HwndOwner As Long, ByRef Titre As String) As String
Dim lpIDList As Long
Dim sBuffer As String
Dim BrowseInfo As T_BROWSEINFO
BrowseFolder = ""
With BrowseInfo
.HwndOwner = HwndOwner
.lpszTitle = lstrcat(Titre, "")
.ulFlags = BIF_RETURNONLYFSDIRS
End With
lpIDList = SHBrowseForFolder(BrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
BrowseFolder = sBuffer
End If
End Function
Is it possible to allow more physical space for "Titre"? I'd like to pass a string with the current default (app's) directory but it is a very long string and doesn't fit even if you break it in 2 lines.
-
Re: Select folder to save into
Quote:
Originally Posted by krtxmrtz
Is it possible to allow more physical space for "Titre"? I'd like to pass a string with the current default (app's) directory but it is a very long string and doesn't fit even if you break it in 2 lines.
Post an example of the directory string, and I'll play around with it.
-
Re: Select folder to save into
Quote:
Originally Posted by Hack
Post an example of the directory string, and I'll play around with it.
Well, I don't have any name handy now, but you can make one up yourself, something just like this:
"c:\MainFolder\SubFolder1\SubFolder2\...\SubFolderN\"
Make it as long as possible so it does not fit in "Titre"... Well, I know one approach could be to use something like EllipseText, but I thought you could configure the dialog so as to place the directory tree window below the "Titre" text.
-
1 Attachment(s)
Re: Select folder to save into
This is as big a path as I could get on it. Do you have paths longer than this?
-
Re: Select folder to save into
ILMV, If I were you I would use the CCRP link the dglienna posted. They have some other good stuff there. If you want to do it without a dependency file I can upload Joacim Andersson's class module that lets you do the same thing without knowing how all the APIs work.
Here is one like the CCRP one, but its from vbAccelerator and you could add all the source code directly to your project: http://www.vbaccelerator.com/home/NE...er/article.asp
-
Re: Select folder to save into
Quote:
Originally Posted by Hack
This is as big a path as I could get on it. Do you have paths longer than this?
Probably not, perhaps occasionally but in this case I think I should ellipse the text and figure out the text length to see if it fits in the label... is it a label that the text is placed in? How do you retrieve its length to be compared to the text length?
-
1 Attachment(s)
Re: Select folder to save into
As you can see by the attached image, it's fully possible to have a sizable browse for folder dialog box with a "Make New Folder" button. As you also can see this will be enough for viewing any path as the title, the example in the image is 260 characters long which is the maximum path length you can have in Windows.
To get this new style add this constant to your declaration section.
VB Code:
Private Const BIF_NEWDIALOGSTYLE = &H40
Then change the following (changes in bold)
VB Code:
Public Function BrowseFolder(ByVal HwndOwner As Long, ByRef Titre As String) As String
Dim lpIDList As Long
Dim sBuffer As String
Dim BrowseInfo As T_BROWSEINFO
BrowseFolder = ""
With BrowseInfo
.HwndOwner = HwndOwner
.lpszTitle = lstrcat(Titre, "")
.ulFlags = BIF_RETURNONLYFSDIRS[b] Or BIF_NEWDIALOGSTYLE [/b]
End With
lpIDList = SHBrowseForFolder(BrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
BrowseFolder = sBuffer
End If
End Function
But wouldn't it be much slicker to initilize the dialog to automatically select the current path and then let the use go from there? This is possible if you add a callback procedure for the SHBrowseForFolder API function. A callback procedure/function must exist in a regular BAS module, so if you have the rest of this code already in a module you can just change the code like this (origional declaration not included here)
VB Code:
Private Declare Function SendMessageString _
Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As String) As Long
Private m_Path As String
Public Function BrowseFolder(ByVal hWndOwner As Long, ByVal sInitDir As String) As String
Dim lpIDList As Long
Dim sBuffer As String
Dim BrowseInfo As T_BROWSEINFO
BrowseFolder = ""
With BrowseInfo
.hWndOwner = hWndOwner
If Len(sInitDir) Then
m_Path = sInitDir
.lpfnCallback = ReturnParam(AddressOf ShowFolderCallback)
End If
.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_NEWDIALOGSTYLE
End With
lpIDList = SHBrowseForFolder(BrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
BrowseFolder = sBuffer
End If
End Function
Private Function ReturnParam(ByVal nAddress As Long) As Long
ReturnParam = nAddress
End Function
Private Function ShowFolderCallback( _
ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal lParam As Long, _
ByVal lpData As Long) As Long
Const WM_USER = &H400
Const BFFM_SETSELECTIONA = (WM_USER + 102)
Const BFFM_INITIALIZED = 1
If uMsg = BFFM_INITIALIZED Then
If Len(m_Path) > 0 Then
SendMessageString hWnd, BFFM_SETSELECTIONA, 1&, m_Path
End If
End If
ShowFolderCallback = 0
End Function
-
1 Attachment(s)
Re: Select folder to save into
Joacim Andersson, with the second version of the code the folder path text is not shown, nor is the "make new folder" button desplayed, as you can see in the attached image. I wonder if you have transcribed in your post above the full code or some part of it may have inadvertently slipped out? Could regional settings have screwed it up?
-
1 Attachment(s)
Re: Select folder to save into
Hmmm....Joacim's code worked just fine for me.
-
Re: Select folder to save into
krtxmrtz, Joacim's Code will not work on Win 98, if by any chance you are using one.
i believe that you need to create your own dialog box for that. if someone can also tell me how to use treeview and list all folders in it. i dont know how to use it and also how to create new folder in it.
-
Re: Select folder to save into
Quote:
Originally Posted by Harsh Gupta
if someone can also tell me how to use treeview and list all folders in it. i dont know how to use it and also how to create new folder in it.
I have a piece of code that will list all of the folders on your hard drive in a treeview, however, this is off topic from what ILMV created this thread for, and we don't want to hijack his thread. If you create a new one on your topic, I'll post what I have.
-
Re: Select folder to save into
Quote:
Originally Posted by Hack
Hmmm....Joacim's code worked just fine for me.
I don't see any path name above the directory tree in your attached image. haven't you selected one yet or is it not working for you either?
-
Re: Select folder to save into
Quote:
Originally Posted by Harsh Gupta
krtxmrtz, Joacim's Code will not work on Win 98, if by any chance you are using one.
So that's why, I should have figured.
-
Re: Select folder to save into
Quote:
Originally Posted by krtxmrtz
I don't see any path name above the directory tree in your attached image. haven't you selected one yet or is it not working for you either?
I didn't select one. I was just testing to ensure the Make New Folder Button was there.
-
Re: Select folder to save into
Quote:
Originally Posted by Hack
If you create a new one on your topic, I'll post what I have.
Harsh Gupta, if you create that new thread please post a link to it here so I can subscribe to it later. Thanks.
-
Re: Select folder to save into
Quote:
Originally Posted by krtxmrtz
Harsh Gupta, if you create that new thread please post a link to it here so I can subscribe to it later. Thanks.
sure i will do it Sir!!
so are you using win 98?
link to my thread
-
Re: Select folder to save into
Quote:
Originally Posted by Hack
I didn't select one. I was just testing to ensure the Make New Folder Button was there.
The "new folder" button is displayed in XP, but the path string isn't! Are you folks sure this feature is working?
-
Re: Select folder to save into
Quote:
Originally Posted by Harsh Gupta
Thank you.
I'm using XP at home but 98 at the office.
-
Re: Select folder to save into
Quote:
Originally Posted by krtxmrtz
The "new folder" button is displayed in XP, but the path string isn't! Are you folks sure this feature is working?
Yes it does. See Post #12 in this thread.
-
Re: Select folder to save into
Quote:
Originally Posted by Hack
Yes it does. See Post #12 in this thread.
Well, actually I was referring to Joacim Andersson's code, that he claims can admit a very long path string.
-
Re: Select folder to save into
The BIF_NEWDIALOGSTYLE flag is only supported in version 5.0 and above of the Shell32.dll file (which I should have mentioned). That version came with Windows 2000. However the callback code will still work. It doesn't show the path as the title, instead it selects the path when you open the dialog, in other words the correct folder will be selected already which means that there is no reason to show the path string. Maybe you can try to use the BIF_EDITBOX flag instead (which is supported from version 4.71 and above, in other words in all versions from Win98 and later). The BIF_EDITBOX flag is already declared in Hack's origional code so you just need to use it.
If you need to build your own dialog box there are several free Folder TreeView controls available that you can use. One is available at the Common Controls Replacement Project website.
-
Re: Select folder to save into
Such a fruitful conversation
-
Re: Select folder to save into
Quote:
Originally Posted by Joacim Andersson
...in other words the correct folder will be selected already which means that there is no reason to show the path string.
This makes sense. It looks, however, like there is some space still reserved for the path string that is now empty, therefore the treeview window could have a smalller top value, i.e. it could be shifted somewhat toward the titlebar.
-
1 Attachment(s)
Re: Select folder to save into
Well, you can still use that area to display any message you like. For example (changes in bold):
VB Code:
Public Function BrowseFolder(ByVal HwndOwner As Long, ByVal sInitDir As String) As String
Dim lpIDList As Long
Dim sBuffer As String
Dim BrowseInfo As T_BROWSEINFO
BrowseFolder = ""
With BrowseInfo
.HwndOwner = HwndOwner
[b] .lpszTitle = lstrcat("Select a new path.", "") [/b]
If Len(sInitDir) Then
m_Path = sInitDir
.lpfnCallback = ReturnParam(AddressOf ShowFolderCallback)
End If
.ulFlags = BIF_RETURNONLYFSDIRS
End With
lpIDList = SHBrowseForFolder(BrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
BrowseFolder = sBuffer
End If
End Function
If you like you can even expand this area with an additional status line. This status line can however only be set in the callback procedure. This could be helpful if you for example want the user to see the currently selected path when they expand and select a new folder in the tree.
VB Code:
Public Function BrowseFolder(ByVal HwndOwner As Long, ByVal sInitDir As String) As String
Dim lpIDList As Long
Dim sBuffer As String
Dim BrowseInfo As T_BROWSEINFO
BrowseFolder = ""
With BrowseInfo
.HwndOwner = HwndOwner
[b] .lpszTitle = lstrcat("Current selected path:", "") [/b]
If Len(sInitDir) Then
m_Path = sInitDir
.lpfnCallback = ReturnParam(AddressOf ShowFolderCallback)
End If
.ulFlags = BIF_RETURNONLYFSDIRS[b] Or BIF_STATUSTEXT [/b]
End With
lpIDList = SHBrowseForFolder(BrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
BrowseFolder = sBuffer
End If
End Function
Private Function ShowFolderCallback( _
ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal lParam As Long, _
ByVal lpData As Long) As Long
Const WM_USER = &H400
Const BFFM_SETSELECTIONA = (WM_USER + 102&)
[b] Const BFFM_SETSTATUSTEXTA = (WM_USER + 100&) [/b]
Const BFFM_INITIALIZED = 1&
[b] Const BFFM_SELCHANGED = 2&
Dim sNewPath As String [/b]
If uMsg = BFFM_INITIALIZED Then
If Len(m_Path) > 0 Then
SendMessageString hWnd, BFFM_SETSELECTIONA, 1&, m_Path
End If
[b] ElseIf uMsg = BFFM_SELCHANGED Then
sNewPath = String(MAX_PATH, vbNullChar)
Call SHGetPathFromIDList(lParam, sNewPath)
SendMessageString hWnd, BFFM_SETSTATUSTEXTA, 0&, sNewPath [/b]
End If
ShowFolderCallback = 0
End Function
The second example will produce the result you see in the attached image, the status text (the actual path) is changed when the selection is changed.
-
Re: Select folder to save into
Is it possible with Hack's code to limit access, so a user can only select a network location in "My network places"?