Good Morning,
I'm trying to use a button to bring in multiple file names (chosen by the user) to a text box so I can then split the string by 'new line character' and use the .LoadFromFile method to attach the files to a new record. When I click the button with the code below nothing happens, however if I remove the while loops and arrays, I'm able to do it with one file. I've been banging my head against my desk and trying to play around with it but I can't make it work. Please help!
This code works to bring the name of one file:
Code:
Private Sub cmdBrowse_Click()
Dim filepath()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Title = "Please select file to attach"
If .show = True Then
filepath = .SelectedItems(1)
Else
Exit Sub
End If
End With
Set fd = Nothing
If Len(filepath) = 0 Then
Debug.Assert "No file selected"
Exit Sub
End If
Me.Attachments = filepath
End Sub
This Code does NOT work:
Code:
Private Sub cmdBrowse_Click()
Dim i, temp
Dim filepath()
Dim fd As FileDialog
i = 1
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Title = "Please select file to attach"
If .show = True Then
While .SelectedItems(i) <> ""
filepath(i) = .SelectedItems(i)
Wend
Else
Exit Sub
End If
End With
Set fd = Nothing
If Len(filepath) = 0 Then
Debug.Assert "No file selected"
Exit Sub
End If
i = 1
While filepath(i) <> ""
temp = Me.Attachments
Me.Attachments = temp & filepath(i) & vbcrlf
Wend
End Sub