Solved

VBA FileDialog - Set Multiple Selected Files to Array

Posted on 2013-11-26
3
1,896 Views
Last Modified: 2013-11-26
I'm trying to enable this code for file picker to select multiple file and return all of the filename.  I've used this code for sometime to return one file.  I'm trying to enable multiselect so I can return a list of selected files.

Can someone help me get there?

Public Function modImportExport_OpenFile(strFileExtension As String, Optional bolAllowMultiSelect As Boolean = False)
    ' the input is used do set the filtering on the dialog box
    ' Returns Filename/Path OR Canceled
    Dim objfd As Object
    Dim vrtSelectedItem As Variant
    Dim strExtType As String
    Set objfd = Application.FileDialog(3)
  
    Select Case strFileExtension
        Case "xls", "xlsx"
            strExtType = "*.xls,*.xlsx"
        Case "txt", "asc", "csv"
            strExtType = "*.txt,*.acs,*.csv,*.prn"
        Case Else
            strExtType = "*.*"
    End Select
    With objfd
        .Title = "Please Select Your File"
        .AllowMultiSelect = bolAllowMultiSelect
        .Filters.Clear
        .Filters.Add "Files", strExtType, 1

        If .show = -1 Then

           For Each vrtSelectedItem In .SelectedItems
             'Return File Name and Path
               modImportExport_OpenFile = vrtSelectedItem  '<< Something needs to happen here to put selected files (1 or more) into an array to return
           Next vrtSelectedItem
                      
        Else
        'The user pressed Cancel.
            modImportExport_OpenFile = "Canceled"
        End If
        
    End With
    'Set the object variable to Nothing.
    Set objfd = Nothing
End Function

Open in new window

0
Comment
Question by:keschuster
3 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
You could insert items into your array at that entry point, or you could concatenate the individual file names to a string, using a separator like ";".  Then after the loop is done, you could simply use the Split( ) function to populate your array:

Dim myArray() as string

'insert other code here

myArray = Split(varFileNames, ";")

I prefer the latter because I don't have to dimension the array beforehand and check to see whether I have exceeded that value.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
try this revised codes

Public Function modImportExport_OpenFile(strFileExtension As String, Optional bolAllowMultiSelect As Boolean = False)
    ' the input is used do set the filtering on the dialog box
    ' Returns Filename/Path OR Canceled
    Dim objfd As Object
    Dim vrtSelectedItem As Variant
    Dim strExtType As String, strFiles As String
    Set objfd = Application.FileDialog(3)
  
    Select Case strFileExtension
        Case "xls", "xlsx"
            strExtType = "*.xls,*.xlsx"
        Case "txt", "asc", "csv"
            strExtType = "*.txt,*.acs,*.csv,*.prn"
        Case Else
            strExtType = "*.*"
    End Select
    With objfd
        .Title = "Please Select Your File"
        .AllowMultiSelect = bolAllowMultiSelect
        .Filters.Clear
        .Filters.Add "Files", strExtType, 1

        If .Show = -1 Then

           For Each vrtSelectedItem In .SelectedItems
             'Return File Name and Path
             strFiles = strFiles & "," & vrtSelectedItem
               modImportExport_OpenFile = vrtSelectedItem  '<< Something needs to happen here to put selected files (1 or more) into an array to return
           Next vrtSelectedItem
              modImportExport_OpenFile = Mid(strFiles, 2)
        Else
        'The user pressed Cancel.
            modImportExport_OpenFile = "Canceled"
        End If
        
    End With
    'Set the object variable to Nothing.
    Set objfd = Nothing
End Function
                                  

Open in new window

0
 

Author Comment

by:keschuster
Comment Utility
so in my case where strFileList() = the array
Insert

           For Each vrtSelectedItem In .SelectedItems
               strFileList = Split(vrtSelectedItem, ";")  '<<< Insert this??
           Next vrtSelectedItem
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now