getting path for folder

J

Joanne

Hi,

I was wondering if there is an easy way to get the path to a folder
using a dialog? I'm looking for something like
..Dialogs(wdDialogFileOpen) but I need to be able to select a floder
rather than a file.

Thanks,

Joanne
 
M

Malcolm Smith

Here's come code which I located years ago on the internet.

- Malc
www.dragondrop.com


Option Explicit


Private Const MAX_PATH = 260



Private Type SHITEMID ' mkid
cb As Long ' Size of the ID (including cb itself)
abID() As Byte ' The item ID (variable length)
End Type


Private Type SHFILEINFO ' shfi
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type

Private Type ITEMIDLIST ' idl
mkid As SHITEMID
End Type

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

' Frees memory allocated by SHBrowseForFolder()
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)


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

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
'
'



Public Function SelectFolder(sCaption As String) As String

Dim BI As BROWSEINFO
Dim nFolder As Long
Dim IDL As ITEMIDLIST
Dim pidl As Long
Dim sPath As String
Dim SHFI As SHFILEINFO

Dim sSelectedFolder As String


On Error Resume Next

sSelectedFolder = ""

With BI
'// The dialog'//s owner window...
'.hOwner = Me.hWnd

'// Initialize the buffer that rtns the display name of the selected
folder
.pszDisplayName = String$(MAX_PATH, 0)

'// Set the dialog'//s banner text
.lpszTitle = sCaption

'// Set the type of folders to display & return
'// -play with these option constants to see what can be returned
'.ulFlags = GetReturnType()

End With

'// if you stop code execution between here and the
'// end of this sub, you will be wasting memory.
'// you need to call CoTaskMemFree pIdl to free the
'// memory used by SHBrowseForFolder

'// Show the Browse dialog
pidl = SHBrowseForFolder(BI)

'// If the dialog was cancelled...
If pidl > 0 Then

'// Fill sPath w/ the selected path from the id list
'// (will rtn False if the id list can'//t be converted)
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList ByVal pidl, ByVal sPath

'// Display the path and the name of the selected folder
sSelectedFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1)

'// Frees the memory SHBrowseForFolder()
'// allocated for the pointer to the item id list
End If
CoTaskMemFree pidl

SelectFolder = sSelectedFolder

End Function
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Joanne > écrivait :
In this message, < Joanne > wrote:

|| Hi,
||
|| I was wondering if there is an easy way to get the path to a folder
|| using a dialog? I'm looking for something like
|| .Dialogs(wdDialogFileOpen) but I need to be able to select a floder
|| rather than a file.
||

Also, if you want to give the user a chance to create a folder on the fly,
try this:

'_______________________________________
Sub SelectFolder()
'Needs a reference to (Tools > Reference)
'Microsoft Shell Controls And Automation
'Simple version

Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder
Dim EverythingOK As Boolean

EverythingOK = False

Set oShell = New Shell32.Shell
Set oFolder = oShell.BrowseForFolder(0, "Select or create a folder", 0)

On Error GoTo NoPath
MsgBox oFolder.Self.Path
EverythingOK = True

NoPath:
Set oFolder = Nothing
Set oShell = Nothing

If EverythingOK Then Exit Sub

On Error GoTo 0
MsgBox "Action cancelled by user.", vbExclamation, "Cancelled"

End Sub
'_______________________________________

Or, if you need it more than once, use a function call like:

'_______________________________________
Sub Test()
MsgBox GetFolderName("Choose a folder") 'Text inside browse for folder
box

End Sub
'_______________________________________

'_______________________________________
Function GetFolderName(sCaption As String) As String
'Needs a reference to (Tools > Reference)
'Microsoft Shell Controls And Automation
Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder
Dim oItems As Shell32.FolderItems
Dim Item As Shell32.FolderItem

On Error GoTo CleanUp

Set oShell = New Shell
Set oFolder = oShell.BrowseForFolder(0, sCaption, 0)
Set oItems = oFolder.Items
Set Item = oItems.Item

GetFolderName = Item.Path

CleanUp:
Set oShell = Nothing
Set oFolder = Nothing
Set oItems = Nothing
Set Item = Nothing

End Function
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi Joanne,
do you mean something like this,
to set the folder for the dialog?
'---
Dim oDlg As Dialog ' Object Dialog
Dim sPth As String ' String Path
sPth = "d:\dat\" '
Set oDlg = Dialogs(wdDialogFileOpen)
oDlg.Name = sPth
oDlg.Show
'---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
M

Martin Seelhofer

Hi Joanne
I'm looking for something like .Dialogs(wdDialogFileOpen) but I
need to be able to select a floder rather than a file

Are you looking for something like this?

Dim path As String
' change the current file open folder
Application.ChangeFileOpenDirectory "D:\temp"

' now display the dialog (don't execute it, just get name)
With Application.Dialogs(wdDialogFileOpen)
If Not .Display Then Exit Sub

' get the current directory (needed since user may have changed dir)
' ... and append the chosen filename
path = CurDir() & "\" & .name
MsgBox path
End With


Cheers,
Martin
 

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