Populate Listbox from OpenFileDialog Multi-Select

Populate Listbox from OpenFileDialog Multi-Select

Anonymous
Not applicable
1,050 Views
2 Replies
Message 1 of 3

Populate Listbox from OpenFileDialog Multi-Select

Anonymous
Not applicable

I really don't think this is too hard, but here it goes. I'll post my code below. I've enabled muti-select. After the user selects whatever files they want, it's supposed to populate the list box. Instead, it concatenates it all onto one line separated by "|" for each file. Any ideas of how to get them to separate?

 

Private Sub cmdSourceAdd_Click()
    Dim oFileDlg As FileDialog
    ' Create a new FileDialog object.
    Call ThisApplication.CreateFileDialog(oFileDlg)

    ' Define the filter to select part and assembly files or any file.
    oFileDlg.Filter = "Inventor Files (*.iam;*.ipt;*.idw;*.dwg)|*.iam;*.ipt;*.idw;*.dwg|All Files (*.*)|*.*"
    ' Define the part and assembly files filter to be the default filter.
    oFileDlg.FilterIndex = 1
    oFileDlg.MultiSelectEnabled = True

    ' Set the title for the dialog.
    oFileDlg.DialogTitle = "Open File Test"

    ' Set the initial directory that will be displayed in the dialog.
    'oFileDlg.InitialDirectory = ThisApplication.FileOptions.ProjectsPath

    ' Set the flag so an error will be raised if the user clicks the Cancel button.
    oFileDlg.CancelError = True

    ' Show the open dialog.  The same procedure is also used for the Save dialog.
    ' The commented code can be used for the Save dialog.
    On Error Resume Next
    oFileDlg.ShowOpen
'    oFileDlg.ShowSave

    ' If an error was raised, the user clicked cancel, otherwise display the filename.
    If Err Then
        MsgBox "User cancelled out of dialog"

    ElseIf Not oFileDlg.fileName = "" Then
        lstSource.AddItem oFileDlg.fileName
        'WHAT GOES HERE???
       
    End If

End Sub

 

0 Likes
Accepted solutions (1)
1,051 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

One thing I tried is below. This sets the program in to an endless loop because it doesn't know how to go to the next file.

 

Private Sub cmdSourceAdd_Click()
    Dim oFileDlg As FileDialog
    ' Create a new FileDialog object.
    Call ThisApplication.CreateFileDialog(oFileDlg)

    ' Define the filter to select part and assembly files or any file.
    oFileDlg.Filter = "Inventor Files (*.iam;*.ipt;*.idw;*.dwg)|*.iam;*.ipt;*.idw;*.dwg|All Files (*.*)|*.*"
    ' Define the part and assembly files filter to be the default filter.
    oFileDlg.FilterIndex = 1
    oFileDlg.MultiSelectEnabled = True

    ' Set the title for the dialog.
    oFileDlg.DialogTitle = "Open File Test"

    ' Set the initial directory that will be displayed in the dialog.
    'oFileDlg.InitialDirectory = ThisApplication.FileOptions.ProjectsPath

    ' Set the flag so an error will be raised if the user clicks the Cancel button.
    oFileDlg.CancelError = True

    ' Show the open dialog.  The same procedure is also used for the Save dialog.
    ' The commented code can be used for the Save dialog.
    On Error Resume Next
    oFileDlg.ShowOpen
'    oFileDlg.ShowSave

    ' If an error was raised, the user clicked cancel, otherwise display the filename.
    If Err Then
        MsgBox "User cancelled out of dialog"

    ElseIf Not oFileDlg.fileName = "" Then
        lstSource.AddItem oFileDlg.fileName
        'WHAT GOES HERE???
        Do Until oFileDlg.fileName = ""
        Loop
        
    End If

End Sub

 

0 Likes
Message 3 of 3

Anonymous
Not applicable
Accepted solution

Shout out to rjay75 for the answer!

 

    ElseIf Not oFileDlg.fileName = "" Then
        Dim fNames As Variant
        fNames = Split(oFileDlg.fileName, "|")
        Dim curName As Variant
        For Each curName In fNames
            lstSource.AddItem curName
        Next curName
    End If