InputBox - Browse

B

banavas

Dear Friends,

how can I add a browse button on an inputbox? I added a default value
(path) but I want to give the user the option to select a file to read
through a browse button.

Thanks,
G.
 
B

Bob Phillips

This code below will run a folder browse dialog and return the selected
folder

This is a demo of how to use it

MsgBox GetFolder

Watch wrap-around.


'==============================
'Code starts here

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) 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

'-------------------------------------------------------------
Function GetFolder(Optional ByVal Name As String = "Select a folder.")
As String
'-------------------------------------------------------------
Dim bInfo As BROWSEINFO
Dim path As String
Dim oDialog As Long

bInfo.pidlRoot = 0& 'Root folder = Desktop

bInfo.lpszTitle = Name

bInfo.ulFlags = &H1 'Type of directory to return
oDialog = SHBrowseForFolder(bInfo) 'display the dialog

'Parse the result
path = Space$(512)

GetFolder = ""
If SHGetPathFromIDList(ByVal oDialog, ByVal path) Then
GetFolder = Left(path, InStr(path, Chr$(0)) - 1)
End If

End Function



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
R

Rob van Gelder

Works OK for the most part, but you can select cells while the dialog is
open.

You can get around it by doing something like:

Private Declare Function FindWindow Lib "user32.dll" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

....

bInfo.hOwner = FindWindow(vbNullString, Application.Caption)


--
Rob van Gelder - http://www.vangelder.co.nz/excel


Bob Phillips said:
This code below will run a folder browse dialog and return the selected
folder

This is a demo of how to use it

MsgBox GetFolder

Watch wrap-around.


'==============================
'Code starts here

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) 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

'-------------------------------------------------------------
Function GetFolder(Optional ByVal Name As String = "Select a folder.")
As String
'-------------------------------------------------------------
Dim bInfo As BROWSEINFO
Dim path As String
Dim oDialog As Long

bInfo.pidlRoot = 0& 'Root folder = Desktop

bInfo.lpszTitle = Name

bInfo.ulFlags = &H1 'Type of directory to return
oDialog = SHBrowseForFolder(bInfo) 'display the dialog

'Parse the result
path = Space$(512)

GetFolder = ""
If SHGetPathFromIDList(ByVal oDialog, ByVal path) Then
GetFolder = Left(path, InStr(path, Chr$(0)) - 1)
End If

End Function



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
S

Stephen Bullen

Hi Banavas,
how can I add a browse button on an inputbox? I added a default value
(path) but I want to give the user the option to select a file to read
through a browse button.

Create your own userform for the input box and instead of a normal text
box, use a ComboBox with the dropdown style set to ellipsis. Then in
the _Dropdown event procedure, add the code to show the Browse dialog.

Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.ie
 
T

Tom Ogilvy

If you want to select a file, it might be just as straight forward to put a
command button on a userform and have the command button issue a

Chdrive "C"
Chdir "C:\My Default Folder"
fName = Application.GetOpenFileName

see help on GetOpenFileName for argument options.

There isn't much you can do with an inputbox itself using built in
capabilities.
 
Top