I need help : VBA send email based on cell condition?

Taha Jawadi

New Member
Joined
Feb 24, 2021
Messages
1
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Dears,
I will be thankful if someone helps me in this .

I want a macro that checks a specific cell value :
For example:

If the cell value is > 70 then
1- Takes a capture of a certain cells range

2- add to outlook email body
3-An email will be sent to the recipient

I have to block of codes and i dont know how to mix them in order to get what i need

First code: Takes a picture of certain cells range

VBA Code:
sub Send_Email_To_Agent()
On Error GoTo ende
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object

Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)

With newEmail
.To = Sheet12.Range("D32").Text
.CC = Sheet12.Range("E33").Text
.BCC = Sheet12.Range("E44").Text
.Subject = "HOLD Performance " & " Date Of Observation : " & Date
.Body = Sheet12.Range("D34").Text & vbNewLine & Sheet12.Range("D35").Text & vbNewLine & Sheet12.Range("D36").Text & vbNewLine & Sheet12.Range("D37").Text
.display

Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor

Sheet12.Range("A30:B40").Copy

pageEditor.Application.Selection.Start = Len(.Body)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
.display
.Send
Set pageEditor = Nothing
Set xInspect = Nothing
End With

Set newEmail = Nothing
Set outlook = Nothing
ende:

End Sub
2 Second code: Loops around emails and send
VBA Code:

Code:
Sub Button1_Click()
Dim olApp As Object
Dim olMailItm As Object
Dim iCounter As Integer
Dim Dest As Variant
Dim SDest As String
' Subject
strSubj = "Your HOLD Score " & TODAY - 1

On Error GoTo dbg
' Create a new Outlook object
Set olApp = CreateObject("Outlook.Application")
For iCounter = 1 To WorksheetFunction.CountA(Columns(1))
' Create a new item (email) in Outlook
Set olMailItm = olApp.CreateItem(0)
strBody = ""
useremail = Cells(iCounter, 1).Value
FullUsername = Cells(iCounter, 2).Value
Status = Cells(iCounter, 4).Value
HOLD = Cells(iCounter, 3).Value
'Make the body of an email
strBody = "Dear " & FullUsername & vbCrLf
strBody = strBody & " Your account in woshub.com domain is in" & HOLD & " state" & vbCrLf
strBody = strBody & "The date and time of the last password change is" & HOLD & vbCrLf
olMailItm.To = useremail
olMailItm.Subject = strSubj
olMailItm.BodyFormat = 1
' 1 – text format of an email, 2 - HTML format
olMailItm.Body = strBody
olMailItm.Send
Set olMailItm = Nothing
Next iCounter
Set olApp = Nothing
dbg:
'Display errors, if any
If Err.Description <> "" Then MsgBox Err.Description
End Sub

The idea is in a nutshell:
====> A macro that checks the cell if it is >70
====> sends an email with a screen shot of a certain cells range


Thank you alot for any possible help
 

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.

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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