Multiple attachment

josros60

Registered User.
Local time
Yesterday, 23:30
Joined
Mar 10, 2011
Messages
73
I have browse button to attach multiple files, even if I select multiple files and only show one in the textbox attachment, if add this line more than once on the send button,
Code:
oEmail.Attachments.Add Me.txtAttachment.Value
when click send it repeats same file,
how can i select multiple files to send.

here are the codes:

Browse Button:

Code:
Private Sub btnBrowse_Click()
Dim fileDiag As FileDialog                           'BROWSE BUTTON
    
    Dim strFile As String
    Dim strFolder As String
    Dim file As Variant
    
    Set fileDiag = FileDialog(msoFileDialogFilePicker)
    
    fileDiag.AllowMultiSelect = True
    If fileDiag.Show Then
        For Each file In fileDiag.SelectedItems
            Me.txtAttachment = file
        Next
    End If
End Sub

Send Button:

Code:
Private Sub btnSend_Click()
Dim oApp As New Outlook.Application
Dim oEmail As Outlook.MailItem
Dim sBody As String
Set oEmail = oApp.CreateItem(olMailItem)
    
If IsNull(Me.txtTo) Then                     ' No email address
MsgBox "Please Select a Customer, if the email field is empty, then edit the customer and add the email by clicking the Edit Customer button at the bottom!", vbOKOnly, "Add the Email address"  ' Tell user
Me.txtCompany.SetFocus ' Focus the control
Exit Sub
 Else
 oEmail.To = Me.txtTo
 oEmail.CC = Me.txtCCEmail & ";" & Me.txtCCEmail2
 oEmail.BCC = "ar@hyperwallet.com"
 End If
 
                                   ' Exit the method
If txtSubject > 0 Then
oEmail.Subject = Me.txtSubject
Else
oEmail.Subject = Me.txtCompany.Column(1) & " " & "-" & "  " & "Invoice #"
  End If
  
If txtBody >= 0 Then
sBody = Me.txtBody
Else
 sBody = "Hi" & " " & Me.txtFirstName & "," & vbCrLf & vbCrLf & "Here is the monthly invoice # for " & Me.txtCompany.Column(1) & "." & " " & "Please confirm if we may collect the payment from your wallet. " & vbCrLf & vbCrLf & "" & "Please contact me if you have any questions.Thanks,"
End If
If Forms![Contact Details]![Auto Collect] = True Then

sBody = "Hi" & " " & Me.txtFirstName & "," & vbCrLf & vbCrLf & "Here is the monthly invoice # for " & Me.txtCompany.Column(1) & "." & " " & " As per your instructions we will withdraw the amount from your loading wallet.  " & vbCrLf & vbCrLf & "" & "Please contact me if you have any questions.Thanks,"

End If
    
  
    
    
   ' sBody = "Hi" & " " & Me.txtFirstName & "," & vbCrLf & vbCrLf & "Here is the monthly invoice # for " & Me.txtCompany.Column(1) & "." & " " & "Please confirm if we may collect the payment from your wallet. " & vbCrLf & vbCrLf & "" & "Please contact me if you have any questions.Thanks,"
    'oEmail.Body =
    If Len(Me.txtAttachment) > 0 Then
        oEmail.Attachments.Add Me.txtAttachment.Value
        
    End If
    With oEmail
      '  If Not IsNull(.To) And Not IsNull(.subject) And Not IsNull(.Body) Then
          .Display
           ' MsgBox "Remember to attach Invoice(s)", vbOKOnly, "Attach Invoice(s)"
       End With
      
   oEmail.Body = sBody
End Sub

thanks
 
Code:
Private Sub btnBrowse_Click()
Dim fileDiag As FileDialog                           'BROWSE BUTTON
    
    Dim strFile As String
    Dim strFolder As String
    Dim file As Variant
  [COLOR=Blue]  Dim strAttach As string[/COLOR]
    Set fileDiag = FileDialog(msoFileDialogFilePicker)
    
    fileDiag.AllowMultiSelect = True
    If fileDiag.Show Then
        For Each file In fileDiag.SelectedItems
            strAttach = strAttach & file & ";"
        Next
    Me.txtAttachment = strAttach
    End If
End Sub
 
Thank you.

I tried when when i select two files it highlight them but when i click ok nothing on textbox attachment, doesn't show any.

and i click send, not files attached.
 
tried it and worked, try adding a breakpoint to your code.
 
Yes, i tried when click send give me Run-time error -2147024894(80070002)

and highlight this line (When click send button):


Code:
 If Len(Me.txtAttachment) > 0 Then
        oEmail.Attachments.Add Me.txtAttachment.Value



thanks again.
 
You need to attach the files one by one, not as a delimited string.

The easiest way is to combine the file select code with your
btnSend_Click code.
ie
For Each file In fileDiag.SelectedItems
oEmail.Attachments.Add = file
Next

Alternatively, create a string array variable at the form level, store the names of the selected files in it, and loop through it in your email code.
 
i guess mr.cronk is right, modify this part with this:
Code:
    If Len(Me.txtAttachment) > 0 Then
 [COLOR=Blue]   Dim varAttachment As Variant
    Dim iLoop As Integer[/COLOR]
    varAttachment = Split(Me.txtAttachment.Value,";")
    For iLoop = LBound(varAttachment) To UBound(varAttachment)
            oEmail.Attachments.Add varAttachment(iLoop)
    Next
        
    End If
 
I tried your suggestion:

Code:
 If Len(Me.txtAttachment) > 0 Then
    Dim varAttachment As Variant
    Dim iLoop As Integer
    varAttachment = Split(Me.txtAttachment.Value,";")
    For iLoop = LBound(varAttachment) To UBound(varAttachment)
            oEmail.Attachments.Add varAttachment(iLoop)
    Next
        
    End If

but i get run-time error

Path does not exist, verify the path is correct.

thanks
 
can you show what is the value of Me.txtAttachment.
 
If fileDiag.SelectedItems is only returning the file name instead of the full path, there will be an error in trying to attach only the file name.
 

Users who are viewing this thread

Back
Top Bottom