PDA

View Full Version : Excel form that attaches file



thedon32
06-19-2013, 09:50 AM
Hi Guys,

I'm in need of some help, I have a spreadsheet that has several command buttons and a number of text boxes, I need to have the ability to attach extra files to this spreadsheet, so that when the user clicks the 'Send Email' button, the email will attach this spreadsheet and the other attachment.

It works to a degree by using the Application.GetopenFilename function and placing the filename into a textbox call txt_attach, however it only works if you are attaching 1 file, I'd like to have the ability to attach multiple file, I thought I had it with this code, but it seems to just keep replacing the file name in the loop, I need it to display all the file name I've selected

Private Sub CommandButton1_Click()
Stop
Dim fullFileName As Variant
Dim I As Integer
fullFileName = Application.GetOpenFilename(MultiSelect:=True)
If IsArray(fullFileName) Then
For I = 0 To UBound(fullFileName)
'ActiveWorkbook.Worksheets("Sheet1").Range("L10") = fullFileName(I)
'fullFileName(I) = ActiveWorkbook.Worksheets("Sheet1").Range("L10")
txt_attach.Value = fullFileName(I)
Next I
Else
txt_attach = ""
End If
End Sub

Hope you guys can me out,

JKwan
06-20-2013, 06:05 AM
you just made a very tiny error, here is the correction:
Private Sub CommandButton1_Click()
Dim fullFileName As Variant
Dim I As Integer
fullFileName = Application.GetOpenFilename(MultiSelect:=True)
If IsArray(fullFileName) Then
For I = LBound(fullFileName) To UBound(fullFileName)
'ActiveWorkbook.Worksheets("Sheet1").Range("L10") = fullFileName(I)
'fullFileName(I) = ActiveWorkbook.Worksheets("Sheet1").Range("L10")
txt_attach.Value = txt_attach.Value & fullFileName(I) & " ; "
Next I
txt_attach.Value = Left(txt_attach.Value, Len(txt_attach.Value) - 3)
Else
txt_attach = ""
End If
End Sub