Solved

VBA FileDialog - Set Multiple Selected Files to Array

Posted on 2013-11-26
3
2,132 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)
ID: 39678664
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 39678672
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
ID: 39678681
so in my case where strFileList() = the array
Insert

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 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