Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 30th, 2006, 09:58 AM
Registered User
Join Date: Jun 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to am6019a
Default browsing for a folder, opening file

Level of experience: What's a workbook

Setup: I am writing a macro to open a bunch of file folders and hunt out specific files

Problem: If the marco can't find the file I want it to let the user browse through the file folders to find the one it wants. I found this bit of code, but it doesn't let me see XL files and it doesn't open files for me. Can anyone see a way to augment it?

Private Const BIF_RETURNONLYFSDIRS As Long = &H1
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 BrowseFolder(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, _

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

End Function
Sub browsefolderfunc()
Dim FName As String
    FName = BrowseFolder("Select a folder", "C:\")
    If FName = "" Then
        MsgBox "You didn't select a folder"
        MsgBox "You selected: " & FName
    End If
End Sub
Old June 30th, 2006, 01:39 PM
Friend of Wrox
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts

This is VB6? VBA behind Excel?
Old July 5th, 2006, 08:26 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts

the code you posted just displays the Select Folder dialog. Once you have the name of the folder, you can use the Dir function to get the files you want.
But more simply you can use the CommonDialog control
Please let us know if you need more input
Old July 6th, 2006, 09:17 AM
Authorized User
Join Date: Apr 2005
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts


I have the same problem, Can u please tell me how to use the Dir function to get the files.

Also How to Process each file in folder.....Loop through the Files collection.

something like this?

Dim objFile
For Each objFile In objFolder.Files
MsgBox objFile.Name

Old July 6th, 2006, 12:15 PM
Friend of Wrox
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts

To use the Dir function, you first invoke it with arguments, and then repeatedly invoke it with no arguments. It returns just the name of the file——no path info. After the last file, it returns an empty string. If it is invoked again after that it raises an error.
    Dim FilNm As String

    FilNm = Dir("C:\Temp\*.tmp")

    Do Until FilNm = ""
        ' Call any processing you want to do here:
        ProcessMyFile FilNm
        FilNm = Dir()
    In the process described above your post ShaileshShinde, Folder objects are not being used (ultimately). The process laid out returns a string into FName.

If you use the Windows Scripting Runtime (FileSystemObject) the Dir() function is not the best approach.
Old February 21st, 2007, 07:09 PM
Registered User
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Back to the original code, the answer is sort of right in front of you...

In the line of code that says:

    Set F = SH.BrowseForFolder(0&, Caption, BIF_RETURNONLYFSDIRS, _

The BIF_RETURNONLYFSDIRS flag ... you guessed it ... returns only directories (folders). That's why your Excel files don't show up.

Change that flag to the other one listed up in the constants section, BIF_BROWSEINCLUDEFILES, and you should see both folders and files.

Similar Threads
Thread Thread Starter Forum Replies Last Post
opening client side folder browser in c# sauravnimesh .NET Framework 2.0 4 November 21st, 2008 07:20 AM
opening a folder location JonniP Access 6 January 5th, 2007 09:01 AM
Opening a folder from VB Axxess Excel VBA 2 September 6th, 2006 05:49 AM
Browsing for a file jroxit Other Programming Languages 1 January 23rd, 2006 01:29 PM
opening folder and return files treasacrowe Classic ASP Basics 3 February 18th, 2005 02:21 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.