Question

VBA: Open File Browser to select path of multiple files

Asked by: rusco

Hello!

I need someone to help me with the code on how to open a file browser dialog box that can be used to select more than one ".xls" file. Similar to the one used in Outlook when selecting attachments that are more than one. I want to be able to get the complete path along with the filename of the selected/highlighted items and put them in a array. Any help would be great :-)

I'm using Windows XP with Access 2000 for this one.

Thanks!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-08-02 at 01:09:32ID21078927
Tags

vba

,

file

,

open

,

access

Topic

Microsoft Access Database

Participating Experts
3
Points
125
Comments
23

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. VBA Excel 2007 - msoFileDialogSaveAs - SaveAs .xls
    Hello Experts, please advice, I have an excel file we use as a template, I had trouble in the past using excel templates so i saved the .xls file as a read-only file. This forces the user to use a different file name when saving. I used the code below to suggest an Initial ...
  2. FileDialog in Outlook VBA
    Set fd = Application.FileDialog(msoFileDialogFilePicker) Does not seem to be supported in Outlook 2003. What is alternative code to display file dialog picker ? Thanks
  3. vba How to highlight a default file when FilePicker Dialog op…
    Can anyone tell me how to highlight a default file when I show the FilePicker dialog, but still leave the other files available for selection? There are multiple files in a folder with the same naming convention. I want to highlight the most recent file as a default, but sti...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Data-ManPosted on 2004-08-02 at 01:56:01ID: 11691764

If you have acccess to Excel 2002 or greater you can use the this code by setting a reference and creating an object variable within your Access 2000 code.  I made a few changes, but this is air code.  If you don't have access to Excel 2002, disregard...you may have to use an API to get the file open dialog.....Mike


Sub UseFileDialogOpen()

    Dim lngCount As Long
    Dim strFileNames() as String
    Dim objExcel As Excel.Application
    Set objExcel = new Excel.Application
    ' Open the file dialog
    With objExcel.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = True
        .Show

        ' Display paths of each file selected
        For lngCount = 1 To .SelectedItems.Count
            ReDim Preserve strFileNames(lngcount-1) 'Array starts at zero.
            strFileNames(lngCount-1) = .SelectedItems(lngCount)
        Next lngCount

    End With
    objExcel.Quit
    set objExcel = nothing
End Sub

 

by: ruscoPosted on 2004-08-02 at 07:09:52ID: 11693793

Hi Mike,

Thanks for the code. Sorry to say that in the environment that we will be using the Access program for there is a possibility that some of the users will not have Excel 2002 installed in their system. Thanks for replying =)

 

by: Data-ManPosted on 2004-08-02 at 09:50:02ID: 11695565

Check out this link....it has the code for an API call.  API's are great....lots of code not so great, but they always work.

http://www.experts-exchange.com/Databases/MS_Access/Q_20116033.html?query=file+open+dialog+api&topics=39

Mike

 

by: jjafferrPosted on 2004-08-02 at 12:24:27ID: 11697091

Hi rusco

Here what you can do:
1- Open the Directory (not File) dialog, to select the Directory where your files are (I can provide code).
2- Once the Directory is selected, use the fso (File System Object) to add the files to a Listbox (I can provide code).
3- In the Listbox properties, set Multiple selection to Yes.
4- Now you can do your Multiple file selection.
5- Make a command button to loop throw the selected Listbox items and put them in a array (I can provide code).

If you always search from the same Directory, then You can Omit #1, and put the Directory in #2,
If you always search for xls files, then you can omit the other types from being built in the Listbox.

jaffer

 

by: jjafferrPosted on 2004-08-02 at 13:16:27ID: 11697672

Hi rusco

When you are lucky, then you are lucky :o)

1-
Make a module, call it MultipleFileSelection, and paste this code there:

Option Explicit

' 28-May-2001
' Valid in VB 6 only!
' When using the Common Dialog control to select multiple files
' from the File Open dialog box, the FileName property of the
' Common Dialog control will return both the path and names of
' the selected files. If the Common Dialog control only allows
' long file names to be selected, the path and file names are
' returned in a null character delimited format, with the path
' listed first, and the files listed subsequently
' This sample shows you how to get the path and the selected files
' Demo by Brian Matumbura
' For comments email: brianm@zncc.co.zw


Public Function GetFiles(Optional ByVal sTitle As String = "Open files...") As String
  ' sTitle: Optional Title of Dialog
  Dim sFilenames As String
  Dim cdlOpen As Object
  On Error GoTo ProcError
  ' Get the desired name using the common dialog
  Set cdlOpen = CreateObject("MSComDlg.CommonDialog")
  ' set up the file open dialog file types
  With cdlOpen
    ' setting CancelError means the control will
    ' raise an error if the user clicks Cancel
    .CancelError = True
    .Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.*.txt|INI Files (*.ini)|*.ini|Images (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif"
    .FilterIndex = 1
    .DialogTitle = sTitle
    .MaxFileSize = &H7FFF ' 32KB filename buffer
    ' set up Common Dialog flags
    ' same as .Flags = cdlOFNHideReadOnly Or cdlOFNPathMustExist Or cdlOFNLongNames Or cdlOFNAllowMultiselect or cdlOFNExplorer
    .Flags = &H4 Or &H800 Or &H40000 Or &H200 Or &H80000
    .ShowOpen
    ' get the selected name
    sFilenames = .FileName
  End With
ProcExit:
  GetFiles = sFilenames
  Set cdlOpen = Nothing
  Exit Function
ProcError:
  If Err.Number = &H7FF3 Then Resume Next 'Cancel selected - Ignore
  MsgBox Err.Description & "(" & Err.Number & ")", vbExclamation, "Open error"
  sFilenames = ""
  Resume ProcExit
End Function


2-
Make a Form from Design view,
Make a Listbox, call it List1,
Make a Lable for the Listbox, call it Label1,
Make a command button, call it command2, and place the following code On Click of Command2:

Private Sub Command2_Click()

  Dim strFileNames() As String
  Dim i As Integer
 
  strFileNames = Split(GetFiles, Chr(0))

    If UBound(strFileNames) > 0 Then

      'Path is stored in index 0 of array files in index 1...
      Label1.Caption = "Files selected from: " & strFileNames(0)
      For i = 1 To UBound(strFileNames)
        If i = 1 Then
            AddFiles = strFileNames(0) & strFileNames(i)
        Else
            AddFiles = AddFiles & ";" & strFileNames(0) & strFileNames(i)
        End If
      Next
    Else
      AddFiles = "(No files selected)"
    End If
   
    Me.List1.RowSource = AddFiles

End Sub

--------------------------------
You can get he Path and File Name you are looking for, like this:

For i = 1 To UBound(strFileNames)
 mtFiles(i)=strFileNames(0) & strFileNames(i)
next i

This way, the array myFiles() will contain all your files.


jaffer

 

by: ruscoPosted on 2004-08-02 at 21:52:32ID: 11700692

Hi Jaffer,

I tried using the code you gave me but I seem to be getting an "ActiveX Component can't create object(429)' error.

Hi Mike,

I like the idea of the API and tried out the code from the link you gave. Unfortunately it only can only select one file from open file browse 'dialog box'. I can't figure out yet how to change the API to make it able to select multiple files.

Thanks!

rusco

 

by: jjafferrPosted on 2004-08-02 at 23:44:39ID: 11701025

rusco

Make sure you have your reference to:

Visual Basic For Applications
Microsoft Access XX.X object Library
Microsoft Visual Basic for Applications Extensibility 5.3

To do the Reference, while in VB, Menu Bar > Tools > References.

If it doesn't work, let me know so that I can upload it to a site where you can download it.

jaffer

 

by: ruscoPosted on 2004-08-03 at 06:13:30ID: 11704015

Hi jaffer,

I added the reference but I'm still getting the same error as before. Any suggestions?

thanks!

 

by: jjafferrPosted on 2004-08-03 at 06:19:56ID: 11704085

 

by: jjafferrPosted on 2004-08-03 at 06:24:39ID: 11704125

if you open the module, you will see this line

    .Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.*.txt|INI Files (*.ini)|*.ini|Images (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif"

it is here where you can either Edit OR Add pointing to *.xls so that you can filter the xls files directly.

BTW, Click on the Open button, the dialog will popup, there you can select your multiple files.

I am using WinME and Access 2k

jaffer

 

by: jjafferrPosted on 2004-08-03 at 06:47:38ID: 11704348

Hi rusco

Were you able to download the file?

jaffer

 

by: ruscoPosted on 2004-08-03 at 07:18:50ID: 11704730

Hi Jaffer!

Just downloaded it now. Will test it out and get back to you about it tomorrow morning as it's kinda late here. Thanks!

rusco

 

by: jjafferrPosted on 2004-08-03 at 07:23:40ID: 11704807

No problem
Nighty night

jaffer

 

by: ruscoPosted on 2004-08-03 at 19:40:19ID: 11711941

Hi Jaffer!

Tried it out just now. Unfortunately it still gives me the same error as I stated above. I tried it out in two pc's (on my personal desktop and the one I use at work). Any ideas?

Thanks!

Mike,

Hi Mike! Any new developments with regards to the API? Thanks!

rusco

 

by: Data-ManPosted on 2004-08-03 at 19:43:31ID: 11711947

Add the ALLOWMULTISELECT flag to the code.  I checked it out and this flag is a constant in the code.  You should be able to add the flag.  You might have to add a loop around the code to return all selected files.

Might be worth while to do a search on google or some other site for the name of the api call and the word ALLOWMULTISELECT.

Mike

 

by: Data-ManPosted on 2004-08-03 at 19:50:35ID: 11711966

 

by: ruscoPosted on 2004-08-03 at 22:03:58ID: 11712459

Mike,

Thanks! I the API to work properly. It's able to now select multiplefiles. Must have missed the ALLOWMULTISELECT flag when I read the API.

 

by: Data-ManPosted on 2004-08-03 at 22:30:14ID: 11712524

Glad you got it to work....using an API means the code will run on almost every machine...you don't have to worry about ocx or controls...Mike

 

by: jjafferrPosted on 2004-08-03 at 23:00:58ID: 11712597

rusco
I am sorry to hear this, as this mdb is working fine with me.
but I gues, like Mike said "API's are great....lots of code not so great, but they always work."
where as other codes depend on some installed components on the pc.

Good job Mike.

jaffer

 

by: LuigiCPosted on 2005-02-28 at 00:32:06ID: 13417940

This is wonderful.

Just one little tweak required if possible.

How do I modify this code to allow the user to select one only access file (.mdb)?

...ie not multiple files and only files of type *.mdb

Cheers

Luigi

 

by: Data-ManPosted on 2005-02-28 at 05:45:22ID: 13419376

Lugi,
  Just don't include the 'ALLOWMULTISELECT' flag when opening the dialog.

Mike

 

by: LuigiCPosted on 2005-02-28 at 09:54:35ID: 13421811

Thanks Mike

That's all it took. I removed that flag and hey presto!

Cheers

Luigi

 

by: Data-ManPosted on 2005-02-28 at 10:02:40ID: 13421896

Glad you got it to work.

Don't forget to close out the question.

Regards,
Mike

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...