VBA Code to mail from excel is attaching all files in range

ram9891273198

New Member
Joined
Oct 3, 2019
Messages
3
Hello Everyone,

This is my first post so thank you very much in advance for any help..

I am trying to send individual mail from excel to three team members. I have member name range in C column, email id range in D column and file location path range in F column. My problem is when code is sending mail to third member it is attaching all three files (Specified in F column row 1 to 3) i mean files of first two members name also attached. In mail to second member it is attaching first two members file and in first mail it is sending it to first person only.

Code:
Mail.AddAttachment Range("F" & i).Value

'For i = 1 To Selection.Row
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
.
Without knowing the files you want to attach, the following should do what you are seeking :

Code:
Option Explicit


Sub SendEmailfromOutlook()




    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range
    Dim Path As String
    Dim i As Integer
    
    Path = Application.ActiveWorkbook.Path
    Set OutApp = CreateObject("Outlook.Application")




    For Each cell In Range("D2:D10")
    If cell.Value <> "" Then
        Set OutMail = OutApp.CreateItem(0)
        
            With OutMail
            
                .To = cell.Value
                
                .Subject = Cells(cell.Row, "E").Value
                
                .Body = "Dear " & Cells(cell.Row, "C").Value & "," _
                & vbNewLine & vbNewLine & _
                "Please find attached a list of overdue invoices. Thank you!"
                
                '.Attachments.Add (Path & "\" & Cells(cell.Row, "E").Value)
                                
                '.Send      'uncomment if you want to auto send email without first reviewing
                .Display    'comment out if you uncomment .Send
                                
            End With
        End If
    Next cell
    
End Sub


Download : https://www.amazon.com/clouddrive/share/POZs2OMJMsRUgUz6I4jApPKDah6QA4xoDS1kbuTaCNB
 
Upvote 0
Hi @Dante Amor,
Thank you very much for your reply.
Please find below my code

Regards Ram

Code:
Sub Sendmail()
    Dim Mail As New Message
    Dim Config As Configuration
    Set Config = Mail.Configuration
    Dim subject As String
    Dim mailbody As String
       
Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = ***
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = "myusername"
Config(cdoSendPassword) = "mypassword"
Config.Fields.Update


ThisWorkbook.Sheets("email").Activate
Range("H2").Select
subject = Range("H2").Value
ActiveSheet.Range("C" & Rows.Count).End(xlUp).Select
For i = 1 To Selection.Row


mailbody = "Dear " & Range("c" & i).Value & "," & Range("h3").Value & vbCrLf & vbCrLf
Mail.To = Range("D" & i).Value
Mail.cc = Range("E" & i).Value
attachment = Range("f" & i).Value
Mail.From = Config(cdoSendUserName)
Mail.subject = Range("c" & i).Value & " " & subject
Mail.HTMLBody = mailbody
Mail.AddAttachment Range("F" & i).Value


On Error Resume Next


Mail.Send


Next i




If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical, "There was an error"
Exit Sub


End If


MsgBox "Your email has been send", vbInformation, "Sent"




End Sub
 
Upvote 0
Dear Logit, Thank you for your reply and code. Actually I don't have outlook so I am sending it without outlook and your code is giving me error. can you help me without outlook option to send individual mails with attachment.
 
Upvote 0
Hi @Dante Amor,
Thank you very much for your reply.
Please find below my code
Regards Ram

I changed some lines, performed tests and it works. Send a single file by mail.

Code:
Sub Sendmail()
  Dim Mail As New Message, Config As Configuration
  Dim subject As String, mailbody As String, i As Long, lr As Long
  
  ThisWorkbook.Sheets("email").Activate
  subject = Range("H2").Value
  lr = Range("C" & Rows.Count).End(xlUp).Row
  For i = 2 To lr
[COLOR=#0000ff]    Set Config = Mail.Configuration[/COLOR]
[COLOR=#0000ff]    Config(cdoSendUsingMethod) = cdoSendUsingPort[/COLOR]
[COLOR=#0000ff]    Config(cdoSMTPServer) = "smtp.gmail.com"[/COLOR]
[COLOR=#0000ff]    Config(cdoSMTPServerPort) = 465[/COLOR]
[COLOR=#0000ff]    Config(cdoSMTPAuthenticate) = cdoBasic[/COLOR]
[COLOR=#0000ff]    Config(cdoSMTPUseSSL) = True[/COLOR]
[COLOR=#0000ff]    Config(cdoSendUserName) = "user@gmail.com"[/COLOR]
[COLOR=#0000ff]    Config(cdoSendPassword) = "pwd"[/COLOR]
[COLOR=#0000ff]    Config.Fields.Update[/COLOR]


    mailbody = "Dear " & Range("c" & i).Value & "," & Range("h3").Value & vbCrLf & vbCrLf
    Mail.To = Range("D" & i).Value
    Mail.cc = Range("E" & i).Value
    Mail.From = Config(cdoSendUserName)
    Mail.subject = Range("c" & i).Value & " " & subject
    Mail.HTMLBody = mailbody
    Mail.AddAttachment Range("F" & i).Value
    On Error Resume Next
    Mail.Send
    On Error GoTo 0
[COLOR=#0000ff]    Set Config = Nothing[/COLOR]
[COLOR=#0000ff]    Set Mail = Nothing[/COLOR]
  Next i
  If Err.Number <> 0 Then
    MsgBox Err.Description, vbCritical, "There was an error"
  Else
    MsgBox "Your email has been send", vbInformation, "Sent"
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top