Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Browse for file and set initial folder to search

I am trying to use the Shell32 Browseforfolder code to browse to a
file. That I can get to work and I can get another version to work to
browse to a folder and to be able to se the initial folder but I cannot
seem to get both to work setting the initial folder to look at and
browsing down to a file.
This is the code which allows me to set the folder:

'First you need to set a reference to the "Microsoft Shell Controls And
Automation" object 'library. In the VBA Editor, go to the Tools menu,
'choose References, and scroll down to this item and put a check next
to it.

Private Const BIF_RETURNONLYFSDIRS As Long = &H1
Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
Private Const BIF_RETURNFSANCESTORS As Long = &H8
Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000
Private Const BIF_BROWSEFORPRINTER As Long = &H2000
Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000
Private Const MAX_PATH As Long = 260


Function BrowsetoSpecificFolder(Optional Caption As String, _
Optional InitialFolder As String) As String

Dim SH As Shell32.Shell
Dim F As Shell32.Folder

Set SH = New Shell32.Shell
Set F = SH.BrowseForFolder(0&, Caption, BIF_RETURNONLYFSDIRS, _
InitialFolder)

If Not F Is Nothing Then
BrowsetoSpecificFolder = F.Items.Item.Path
End If

End Function


'You can the call the BrowseFolder function with the following code:
Sub callfolder()
Dim FName As String
FName = BrowsetoSpecificFolder("Select a folder", "C:\aatest")
If FName = "" Then
MsgBox "You didn't select a folder"
Else
MsgBox "You selected: " & FName
End If

End Sub

I have tried adding BIF_BROWSEINCLUDEFILES instead of
BIF_RETURNONLYFSDIRS but I get an automation error can't find file.
Can anyone help?
Many thanks
John

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Browse for file and set initial folder to search

Are you using this for selecting the file eventually? When I want to browse
for a file I generally use the Dialog box, have you thought about this? You
can use a filter to select specific types of files as well.

" wrote:

I am trying to use the Shell32 Browseforfolder code to browse to a
file. That I can get to work and I can get another version to work to
browse to a folder and to be able to se the initial folder but I cannot
seem to get both to work setting the initial folder to look at and
browsing down to a file.
This is the code which allows me to set the folder:

'First you need to set a reference to the "Microsoft Shell Controls And
Automation" object 'library. In the VBA Editor, go to the Tools menu,
'choose References, and scroll down to this item and put a check next
to it.

Private Const BIF_RETURNONLYFSDIRS As Long = &H1
Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
Private Const BIF_RETURNFSANCESTORS As Long = &H8
Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000
Private Const BIF_BROWSEFORPRINTER As Long = &H2000
Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000
Private Const MAX_PATH As Long = 260


Function BrowsetoSpecificFolder(Optional Caption As String, _
Optional InitialFolder As String) As String

Dim SH As Shell32.Shell
Dim F As Shell32.Folder

Set SH = New Shell32.Shell
Set F = SH.BrowseForFolder(0&, Caption, BIF_RETURNONLYFSDIRS, _
InitialFolder)

If Not F Is Nothing Then
BrowsetoSpecificFolder = F.Items.Item.Path
End If

End Function


'You can the call the BrowseFolder function with the following code:
Sub callfolder()
Dim FName As String
FName = BrowsetoSpecificFolder("Select a folder", "C:\aatest")
If FName = "" Then
MsgBox "You didn't select a folder"
Else
MsgBox "You selected: " & FName
End If

End Sub

I have tried adding BIF_BROWSEINCLUDEFILES instead of
BIF_RETURNONLYFSDIRS but I get an automation error can't find file.
Can anyone help?
Many thanks
John


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Browse for file and set initial folder to search

Yes I want to find the file. I have used the dialog box but can you
specify a starting folder for it?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Browse for file and set initial folder to search

The code that you are using is for folders (directories) only.

Try the getopenfilename method:

Sub filetest()
Dim fileToOpen As Variant
fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen < False Then MsgBox "Open " & fileToOpen
End Sub

--
HTHs Martin


" wrote:

Yes I want to find the file. I have used the dialog box but can you
specify a starting folder for it?


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
Browse for folder Steph[_6_] Excel Programming 1 December 8th 05 04:53 PM
Browse for folder - Jim Rech's MD Excel Programming 4 February 8th 05 10:29 PM
Jim Rech's Browse for Folder Keb[_2_] Excel Programming 0 July 14th 04 02:53 PM
Browse for folder MD Excel Programming 1 July 13th 04 01:23 PM
Browse For Folder start directory Todd Huttenstine Excel Programming 2 May 12th 04 02:24 PM


All times are GMT +1. The time now is 04:43 AM.

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"