Outlook, returning email address on IF

markvenis

Board Regular
Joined
Sep 17, 2003
Messages
60
I am trying to get a macro to send an email to all emails in range where the cell to the left of the address = N but seem to be going round in circles and not quite getting it to work.

At the moment I am using the following code, all the Outlook bits work fine i.e. opening the template adding the recipient etc it is setting the recipo value I am having problems with.

The data on sheet CB Emails is as follows

A B

me@mycompany1.com N
me1@mycompany1.com Y
me2@mycompany1.com N

I would like to send an individual mail to each recipient where B=N, my current code is as follows.

_____________________________________________________

Sub chase()

  • Sheets("CB Emails").Select
    For pers = 1 To Range("A65536").End(xlUp).Row
    Range("pers").Activate
    Reply = ActiveCell.Offset(rowOffset:=0, columnOffset:=1).Valu
    recip = pers.Value

If Reply = N Then

'Template email to be used'
Const MailTemplate = "P:\HR Operations\HRonly\HR Database Reporting\Cust Branch flags at payroll\Cap Badgeing\CapBadge Exercise Follow Up.oft"

'Accounts for Errors in Outlook'
On Error Resume Next
Set appOut = GetObject(, "Outlook.Application")
If appOut Is Nothing Then
Set appOut = CreateObject("Outlook.Application.")
blnCreate = True
If appOut Is Nothing Then
MsgBox "Unable to start Outlook.", vbOKOnly + vbCritical, "Send Mail"
Exit Sub
End If
End If
On Error GoTo 0

On Error Resume Next
Set OutMail = appOut.CreateItemFromTemplate(MailTemplate)
If OutMail Is Nothing Then
MsgBox "Unable to create item.", vbOKOnly + vbCritical, "Send Mail"
If blnCreate Then appOut.Quit
Set appOut = Nothing
Exit Sub
End If
On Error GoTo 0


'Attaches workbook to email, adds addresses and sends email'
With OutMail
.Recipients.Add recip
.Send
End With


If blnCreate Then appOut.Quit
Set OutMail = Nothing
Set appOut = Nothing

ActiveWindow.Close

End If

Next pers

End Sub


 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I don't know if you still need help with this or not.
I haven't actually tested this in e-mail but I have some things for you to try if you want:


The first commented out section, replace with this. Change the value in the Dim statement if more than 1000 e-mail addresses is a possibility.

Dim sendone(1000) As String
ct = 0
Sheets("CB Emails").Select
For pers = 1 To Range("A65536").End(xlUp).Row
Reply = Cells(pers, 2).Value
If Reply = "N" Then
ct = ct + 1
sendone(ct) = Cells(pers, 1).Value
End If
Next pers

In the 'Attaches workbook to email... section, replace the code with this:

'Attaches workbook to email, adds addresses and sends email'
With OutMail
For x = 1 To ct
.Recipients.Add sendone(x)
Next x
.Send
End With

Remove the Next pers before the End sub
 
Upvote 0
EDIT: Sorry, didn't see that you wanted individual emails, try this in this section instead:

'Attaches workbook to email, adds addresses and sends email'
With OutMail
For x = 1 To ct
.Recipients.Add sendone(x)
.Send
Next x
End With
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,181
Members
449,071
Latest member
cdnMech

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