Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Let a user choose a folder?

I'm using Excel 2007 with VBA. Does Excel and/or VBA
have any custom dialog boxes that allow a user to select
a folder and/or folder path?? I dont want the user to
type in a folder path, so I'm looking for an easy way to
let a user select a folder with a built-in dialog box or
form or something.

Please give me any suggestions. Thank you!

Robert


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Let a user choose a folder?

Robert Crandal presented the following explanation :
I'm using Excel 2007 with VBA. Does Excel and/or VBA
have any custom dialog boxes that allow a user to select
a folder and/or folder path?? I dont want the user to
type in a folder path, so I'm looking for an easy way to
let a user select a folder with a built-in dialog box or
form or something.

Please give me any suggestions. Thank you!

Robert


This will display the dialog to allow user to choose a folder. It
returns the full path.

With Application.FileDialog(4) 'msoFileDialogFolderPicker
If .Show = False Then Exit Function 'User cancelled
sPath = .SelectedItems(1)
End With

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Let a user choose a folder?

I put this code in a Sub and I got an error message that
said something like "Exit not allowed in Sub". Should
I replace that "Exit Function" code with something else?
What is that code for anyways?

Thanks!


"GS" wrote in message
...

This will display the dialog to allow user to choose a folder. It returns
the full path.

With Application.FileDialog(4) 'msoFileDialogFolderPicker
If .Show = False Then Exit Function 'User cancelled
sPath = .SelectedItems(1)
End With

--
Garry


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Let a user choose a folder?

Robert Crandal used his keyboard to write :
I put this code in a Sub and I got an error message that
said something like "Exit not allowed in Sub". Should
I replace that "Exit Function" code with something else?
What is that code for anyways?

Thanks!


Hi Robert,

That code is a snippet from a function I reuse in many projects, which
returns the full path to the user's selected folder. (Thus 'End
Function') There's no value to putting this code in a sub because you'd
have to repeat for every procedure you want to get a folder path for.
the function is also configured to use the folder picker APIs for
earlier versions that don't support FileDialog.

Here's my function and its required declarations. You can just copy
this into a standard module that you can drop into any project!
(Watch for word wrapping)

<API Declarations
'32-bit API declarations required for folder selection
Public Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias
"SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As
Long
Public Declare Function SHBrowseForFolder Lib "shell32.dll" Alias
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

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

Const mszPickFolder As String = "Select the folder where the files are
located."


<Functions
Function szPickFolder() As String
' Allows the user to select a folder location.

Const sSource As String = "szPickFolder()"

Dim sPath As String

If Application.Version 9 Then '//use the newer dialog
With Application.FileDialog(4) '//msoFileDialogFolderPicker
If .Show = False Then Exit Function 'User cancelled
sPath = .SelectedItems(1)
End With
Else '//use the API dialog
sPath = GetDirectory(mszPickFolder)
End If
If sPath = "" Then Exit Function 'User cancelled
szPickFolder = sPath
End Function '//szPickFolder()

Function GetDirectory(Optional msg As String) As String
' Opens the dialog for picking the folder

Dim bInfo As BROWSEINFO, sPath As String
Dim r As Long, x As Long

If msg = "" Then msg = "Select a folder."

With bInfo
.pidlRoot = 0& '//root folder = Desktop
.lpszTitle = msg '//title the dialog
.ulFlags = &H1 '//type of directory to return
End With
x = SHBrowseForFolder(bInfo) '//display the dialog

'Parse the result
sPath = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal sPath)
If r Then
GetDirectory = Left(sPath, InStr(sPath, Chr$(0)) - 1)
Else
GetDirectory = ""
End If
End Function '//GetDirectory


<To use this code from any procedure
Dim sPathToFolder As String
sPathToFolder = szPickFolder

HTH

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Choose folder with largest filename number SteveZmyname Excel Programming 2 March 13th 10 12:17 PM
User selection of folder and open all .xls files within folder Barb Reinhardt Excel Programming 4 April 14th 07 01:41 PM
How to List the names of the subfolders present in the folder (path of folder is given in the textbox by user ) divya Excel Programming 3 November 30th 06 11:34 AM
How to choose a folder Ctech[_37_] Excel Programming 2 November 4th 05 12:36 PM
ENTER DATE, CHOOSE WKSHEET, SAVE IN FOLDER jlegoland4 Excel Programming 2 September 1st 04 01:09 PM


All times are GMT +1. The time now is 04:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"