Auto mail to take up email address from worsheet & send

Mahesh Rana

New Member
Joined
Aug 2, 2018
Messages
6
Hi!

VBA to send auto mail by extracting mail address from worksheet, BUT
Not able to extract address from the whole column. It works - but on single cell if I write Range ("A1").
Pls. suggest the correction to be done (I think it should be in - marked as Red)

Rich (BB code):
Option Explicit


Private Sub CommandButton1_Click()
    On Error GoTo ErrHandler
    
    ' SET Outlook APPLICATION OBJECT.
    Dim objOutlook As Object
    Set objOutlook = CreateObject("Outlook.Application")
        
    ' CREATE EMAIL OBJECT.
    Dim objEmail As Object
    Set objEmail = objOutlook.CreateItem(0)
       
    On Error Resume Next
    With objEmail
    .To = Range("A:A")
    .CC = Range("B:B")
    .BCC = Range("C:C")
    .Subject = "COLD CHAIN DISPATCHES" & Date
    .Body = "Pls. have the details in attachment."
    .Attachments.Add ("C:\COLD CHAIN.xlsx")
    .Send
End With
    
    ' CLEAR.
    Set objEmail = Nothing:    Set objOutlook = Nothing
        
ErrHandler:
    '
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this one. It will check last row with email address in column A and will loop through every row starting from row 2 through last row.
Note. I haven't checked this modifications.
Changes I made are in RED.

Code:
Option Explicit



Private Sub CommandButton1_Click()
    On Error GoTo ErrHandler
[COLOR=#ff0000]Dim lrow as Long 'last row[/COLOR]

[COLOR=#ff0000]with activesheet
      lrow = .Range("A" & .Rows.Count).End(xlUP).Row
end with[/COLOR]
    
    ' SET Outlook APPLICATION OBJECT.
    Dim objOutlook As Object
    Set objOutlook = CreateObject("Outlook.Application")
        
    ' CREATE EMAIL OBJECT.
    Dim objEmail As Object
    Set objEmail = objOutlook.CreateItem(0)

[COLOR=#ff0000]for i = 2 to lrow[/COLOR]
       
    On Error Resume Next
    With objEmail
[COLOR=#ff0000]    .To = Range("A" & i)[/COLOR]
[COLOR=#ff0000]    .CC = Range("B" & i)[/COLOR]
[COLOR=#ff0000]    .BCC = Range("C" & i)[/COLOR]
    .Subject = "COLD CHAIN DISPATCHES" & Date
    .Body = "Pls. have the details in attachment."
    .Attachments.Add ("C:\COLD CHAIN.xlsx")
    .Send
    End With

[COLOR=#ff0000]next i[/COLOR]
    
    ' CLEAR.
    Set objEmail = Nothing:    Set objOutlook = Nothing
        
ErrHandler:
    '
End Sub
 
Upvote 0
Thanks nardagus for your support.
But their is some issue.
I Had made a small intrusion in Red.
But is sends mail for only the 2nd row of all three columns. I need to send to all the persons in those three columns.


Code:
Option Explicit



Private Sub CommandButton1_Click()
    On Error GoTo ErrHandler
Dim lrow as Long 'last row

with activesheet
      lrow = .Range("A" & .Rows.Count).End(xlUP).Row
end with
    
    ' SET Outlook APPLICATION OBJECT.
    Dim objOutlook As Object
    Set objOutlook = CreateObject("Outlook.Application")
        
    ' CREATE EMAIL OBJECT.
    Dim objEmail As Object
    Set objEmail = objOutlook.CreateItem(0)

[COLOR=#ff0000]Dim I As Integer[/COLOR]
for i = 2 to lrow
       
    On Error Resume Next
    With objEmail
    .To = Range("A" & i)
    .CC = Range("B" & i)
    .BCC = Range("C" & i)
    .Subject = "COLD CHAIN DISPATCHES" & Date
    .Body = "Pls. have the details in attachment."
    .Attachments.Add ("C:\COLD CHAIN.xlsx")
    .Send
    End With

next i
    
    ' CLEAR.
    Set objEmail = Nothing:    Set objOutlook = Nothing
        
ErrHandler:
    '
End Sub
 
Upvote 0
My bad. LOOP start was in a wrong place in a code.

Code:
Private Sub CommandButton1_Click()On Error GoTo ErrHandler
Dim lrow As Long 'last row


With ActiveSheet
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row
End With


' SET Outlook APPLICATION OBJECT.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")


' CREATE EMAIL OBJECT.
Dim objEmail As Object
Dim I As Integer


For I = 2 To lrow


    Set objEmail = objOutlook.CreateItem(0)
    
    On Error Resume Next
    With objEmail
        .To = Range("A" & I)
        .CC = Range("B" & I)
        .BCC = Range("C" & I)
        .Subject = "COLD CHAIN DISPATCHES" & Date
        .Body = "Pls. have the details in attachment."
        .Attachments.Add ("C:\COLD CHAIN.xlsx")
        .Send
    End With


Next I


' CLEAR.
Set objEmail = Nothing: Set objOutlook = Nothing


ErrHandler:
'
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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