Pick data from cell and add to e-mail script

aliaslamy2k

Active Member
Joined
Sep 15, 2009
Messages
416
Office Version
  1. 2019
Platform
  1. Windows
Dear Experts

Below is the e-mail script which is working very well. However, I want the e-mail script to add subject line and take data from cells

Examples :-

strsub = "Alert Vehicles number 12345 (Pick up from cell E2) License Expire in 45 (Pick up from cell J2) days, “


strbody = "Dear All" & vbNewLine & vbNewLine & _
"This is an Auto Generated E-mail" & vbNewLine & vbNewLine & _
"Kindly note that Vehicle number 12345 (Pick up from cell E2) License Expire in 45 (Pick up from cell J2) days, “ & vbNewLine & vbNewLine & _
"Kind Regards," & vbNewLine & vbNewLine & _
"Vehicle Management Team"


Expected Result

Strsub = Alert Vehicle number 12345 License Expires in 45 days

Strbody = Dear All,
This is an Auto Generated E-mail
Kindly note that Vehicle number 12345 License expires in 45 days
Kind regards,
Vehicle Management Team



Actual VBA currently in use


Private Sub Worksheet_Calculate()
Dim c As Range
Application.EnableEvents = False
For Each c In Range("J2:J12")
With c
If .Value > 1 And .Value < 45 And .Offset(0, 1) <> "E-MAIL SENT" Then
Call Semail1
.Offset(0, 1).Value = "E-MAIL SENT"
End If
If .Value > 44 Then .Offset(0, 1).ClearContents
End With
Next c
Application.EnableEvents = True
End Sub





Sub Semail1()

Dim OutApp As Object
Dim OutMail As Object
Dim strto As String, strcc As String, strbcc As String
Dim strsub As String, strbody As String
Application.EnableEvents = False
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strto = "alia@something.com"
strcc = ""
strbcc = ""
strsub = "Vehicles License Expiry Alert, "
strbody = "Dear All" & vbNewLine & vbNewLine & _
"This is an Auto Generated E-mail" & vbNewLine & vbNewLine & _
"Kindly note that one or more vehicle license/s will be expiring within 45 Days, please start planning accordingly" & vbNewLine & vbNewLine & _
"Kind Regards," & vbNewLine & vbNewLine & _
"Vehicle Management Team"
With OutMail
.To = strto
.CC = strcc
.BCC = strbcc
.Subject = strsub
.Body = strbody
.Display
Application.Wait (Now + TimeValue("0:00:02"))
Application.SendKeys "%s"
End With
Application.EnableEvents = True
Set OutMail = Nothing
Set OutApp = Nothing
End Sub


ALI - EMAIL TESTING 18MAR20.xlsm
EFGHIJK
1Vehicle #OwnerUser CompanyVehicle BrandQID EXPIRY DATE Days to expireStatus
212345AAAAAToyota15-Apr-202028E-MAIL SENT
36789BBBBBKia26-Mar-2022738
4101112CCCCCMitsubishi30-Jul-2019LIC EXPIRED
5131415DDDDDToyota13-Apr-2019LIC EXPIRED
6161718EEEEEKia28-Dec-2020285
7192021FFFFFMitsubishi24-Mar-20206E-MAIL SENT
8222324GGGGGToyota16-Sep-2020182
9252627HHHHHKia18-Jul-2020122
10282930IIIIIMitsubishi18-Mar-20200
11313233JJJJJToyota7-Apr-202020E-MAIL SENT
12343536KKKKKKia7-Dec-2019LIC EXPIRED
Sheet1
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try this

VBA Code:
Private Sub Worksheet_Calculate()
  Dim c As Range
  Application.EnableEvents = False
  For Each c In Range("J2:J12")
    With c
      If .Value > 1 And .Value < 45 And .Offset(0, 1) <> "E-MAIL SENT" Then
        Call Semail1(c.Row)
        .Offset(0, 1).Value = "E-MAIL SENT"
      End If
      If .Value > 44 Then .Offset(0, 1).ClearContents
    End With
  Next c
  Application.EnableEvents = True
End Sub

Sub Semail1(i As Long)
  Dim OutApp As Object
  Dim OutMail As Object
  Dim strto As String, strcc As String, strbcc As String
  Dim strsub As String, strbody As String
  Application.EnableEvents = False
  Set OutApp = CreateObject("Outlook.Application")
  OutApp.Session.Logon
  Set OutMail = OutApp.CreateItem(0)
  
  strto = "alia@something.com"
  strcc = ""
  strbcc = ""
  strsub = "Alert Vehicles number " & Range("E" & i) & " License Expire in " & Range("J" & i) & " days, "
  strbody = "Dear All" & vbNewLine & vbNewLine & _
    "This is an Auto Generated E-mail" & vbNewLine & vbNewLine & _
    "Kindly note that Vehicle number " & Range("E" & i) & " License Expire in " & Range("J" & i) & " days, " & vbNewLine & vbNewLine & _
    "Kind Regards," & vbNewLine & vbNewLine & _
    "Vehicle Management Team"
  With OutMail
    .To = strto
    .CC = strcc
    .BCC = strbcc
    .Subject = strsub
    .Body = strbody
    .Display
    Application.Wait (Now + TimeValue("0:00:02"))
    Application.SendKeys "%s"
  End With
  Application.EnableEvents = True
  Set OutMail = Nothing
  Set OutApp = Nothing
End Sub
 
Upvote 0
Try this

VBA Code:
Private Sub Worksheet_Calculate()
  Dim c As Range
  Application.EnableEvents = False
  For Each c In Range("J2:J12")
    With c
      If .Value > 1 And .Value < 45 And .Offset(0, 1) <> "E-MAIL SENT" Then
        Call Semail1(c.Row)
        .Offset(0, 1).Value = "E-MAIL SENT"
      End If
      If .Value > 44 Then .Offset(0, 1).ClearContents
    End With
  Next c
  Application.EnableEvents = True
End Sub

Sub Semail1(i As Long)
  Dim OutApp As Object
  Dim OutMail As Object
  Dim strto As String, strcc As String, strbcc As String
  Dim strsub As String, strbody As String
  Application.EnableEvents = False
  Set OutApp = CreateObject("Outlook.Application")
  OutApp.Session.Logon
  Set OutMail = OutApp.CreateItem(0)
 
  strto = "alia@something.com"
  strcc = ""
  strbcc = ""
  strsub = "Alert Vehicles number " & Range("E" & i) & " License Expire in " & Range("J" & i) & " days, "
  strbody = "Dear All" & vbNewLine & vbNewLine & _
    "This is an Auto Generated E-mail" & vbNewLine & vbNewLine & _
    "Kindly note that Vehicle number " & Range("E" & i) & " License Expire in " & Range("J" & i) & " days, " & vbNewLine & vbNewLine & _
    "Kind Regards," & vbNewLine & vbNewLine & _
    "Vehicle Management Team"
  With OutMail
    .To = strto
    .CC = strcc
    .BCC = strbcc
    .Subject = strsub
    .Body = strbody
    .Display
    Application.Wait (Now + TimeValue("0:00:02"))
    Application.SendKeys "%s"
  End With
  Application.EnableEvents = True
  Set OutMail = Nothing
  Set OutApp = Nothing
End Sub
Hi DanteAmor,

This code is Perfect !!!

Thanks a million for this wonderful code. It works exactly as expected.

Thank you once again .

Regards,
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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