PDA

View Full Version : Browsing for a Folder



spencer.67965
2004-07-28, 10:11 PM
Hi all,

I was wondering if there is any way I can browse for a folder using VBA. I am trying to set a string to a user defined folder. I noticed there is getfiled which works great for files but was wondering if there is a similar function for folders.

Thanks in advance,

Spencer

AutoCAD 2005, Win 2k

ntaylor
2004-07-29, 06:04 AM
You should be able to find something here to use.
http://www.google.com.au/search?hl=en&ie=UTF-8&q=%22folder+browser%22+activex&meta=lr%3Dlang_en

jwanstaett
2004-07-29, 01:57 PM
I made a UserControl that I use to Browse for floder

see DirList.ocx in Attach zip file

spencer.67965
2004-07-29, 02:43 PM
Thanks,

Spencer

Ed Jobe
2004-07-29, 03:22 PM
All you need is the windows api.


'API Constants
Private Const MAX_PATH = 2600

'API Types
Private 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

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

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long


Private Function ReturnFolder(DialogTitle As String) As String
Dim Browser As BROWSEINFO
Dim lngFolder As Long
Dim strPath As String
On Error GoTo Err_Control
With Browser
.hOwner = 0&
.lpszTitle = DialogTitle
.pszDisplayName = String(MAX_PATH, 0)
End With
strPath = String(MAX_PATH, 0) '<-- VERY Important!!
lngFolder = SHBrowseForFolder(Browser)
If lngFolder Then
SHGetPathFromIDList lngFolder, strPath
ReturnFolder = Left(strPath, InStr(strPath, vbNullChar) - 1)
End If
Exit_Here:
Exit Function
Err_Control:
Select Case Err.Number
Case Else
MsgBox Err.Description
Err.Clear
Resume Exit_Here
End Select
End Function

spencer.67965
2004-07-29, 11:03 PM
Thanks again for all your help, you all make this learing experience quite easy and enjoyable.

Spencer

jwanstaett
2004-08-02, 01:33 PM
I like the way the API worked so over the weekend I make a use control that use the API the control as a label textbox and Commandbuton on it

see attached file

jwanstaett
2005-08-19, 03:56 PM
you can use the Shell to Browse For a Folder
in autocad vba


'use the shell to browseforfolder
'Return "" if no folder
'return the path to the floder
Public Function GetFolder(Title As String) As String
Dim myShell
Dim myFolder
Dim myFolderItem
Dim myPath
Set myShell = AcadApplication.GetInterfaceObject("shell.Application")
Set myFolder = myShell.BrowseForFolder(0, Title, 0, 0)
If myFolder Is Nothing Then
GetFolder = ""
Else
Set myFolderItem = myFolder.Self
GetFolder = myFolderItem.Path
End If
End Function


or do it in Lisp
Note: will error if on folder is selected


(vl-load-com)
(defun getFolder()
(if (= myshell nil)
(setq myshell (vlax-create-object "shell.Application"))
()
)
(setq myfolder (vlax-invoke-method myshell "BrowseForFolder" 0 "tite"0))
(setq myfolderI(vlax-get-property myfolder "self"))
(setq mypaht (vlax-get-property myfolderi "path"))
)

mtuersley
2005-08-19, 04:37 PM
The shell option was already suggested and the suggestion to use the api is always preferable over using GetInterfaceObject. Less "layers" to go through to get the same result.

jason907238
2007-08-02, 08:37 PM
Is there a way to get Ed's code to start from a different director (path) than MyComputer?

JIM651427
2011-05-10, 02:15 PM
Hi, thank you for Dirlist.zip.

i think i can use this in a routine i am trying to write. i appreciate you posting it. i have one question. I do not seem to be able to get a dblclick event to work with this control. i am simply trying to display the contents of a folder in a separate listbox when i dblclick on the folder in this control. i am sure i am just not thinking clearly and forgetting something but thought you might could help.

thanks in advance
jimd

arshiel88
2011-05-11, 08:10 AM
Is there a way to get Ed's code to start from a different director (path) than MyComputer?

Yes. Just add the following...


Public Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hWndOwner As Long, ByVal nFolder As Long, ListId As Long) As Long

Public Enum FolderType
CSIDL_BITBUCKET = 10
CSIDL_CONTROLS = 3
CSIDL_DESKTOP = 0
CSIDL_DRIVES = 17
CSIDL_FONTS = 20
CSIDL_NETHOOD = 18
CSIDL_NETWORK = 19
CSIDL_PERSONAL = 5
CSIDL_PRINTERS = 4
CSIDL_PROGRAMS = 2
CSIDL_RECENT = 8
CSIDL_SENDTO = 9
CSIDL_STARTMENU = 11
End Enum

..and the following inside the function


SHGetSpecialFolderLocation hWndOwner, CSIDL_PERSONAL, RootID

With Browser
.hOwner = 0&
.lpszTitle = DialogTitle
.pszDisplayName = String(MAX_PATH, 0)
.pidlRoot = RootID
End With