How can I send a mail with a condition? VBA

cynthixie

New Member
Joined
Aug 17, 2022
Messages
12
Office Version
  1. 2019
Platform
  1. Windows
I would like the message to be sent when there is a value in column H, for example "y"

1660750882027.png


My code is:

Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(oMailItem)
Dim strMailBody As String

intRow = 2
strISO = ThisWorkbook.Sheets("MS_Data").Range("B" & intRow).Text

While (strISO <> "")

Set objEmail = objOutlook.CreateItem(oMailItem)

StrMailSubject = ThisWorkbook.Sheets("Mail_Details").Range("A2").Text
strMailBody = "<BODY style='font-size:11pt;font-family:Calibri(Body)'>" & ThisWorkbook.Sheets("Mail_Details").Range("B2").Text & "</BODY>"
strMailBody = Replace(strMailBody, Chr(10), "<br>")

strFolder = "C:\Users\CIOTTIC\OneDrive - IAEA\Desktop\AL TEST"
strISO = ThisWorkbook.Sheets("MS_Data").Range("B" & intRow).Text
strSalutation = ThisWorkbook.Sheets("MS_Data").Range("C" & intRow).Text
strEmail = ThisWorkbook.Sheets("MS_Data").Range("D" & intRow).Text
strCC = ThisWorkbook.Sheets("MS_Data").Range("E" & intRow).Text
strFile = ThisWorkbook.Sheets("MS_Data").Range("F" & intRow).Text
strFile2 = ThisWorkbook.Sheets("MS_Data").Range("G" & intRow).Text

StrMailSubject = Replace(StrMailSubject, "<ISO>", strISO)
strMailBody = Replace(strMailBody, "<Salutation>", strSalutation)

With objEmail
.To = CStr(strEmail)
.CC = CStr(strCC)
.Subject = StrMailSubject
.BodyFormat = olFormatHTML
.Display
.Attachments.Add strFolder & "\" & strFile
.Attachments.Add strFolder & "\" & strFile2
.HTMLBody = strMailBody & .HTMLBody
.Send

End With

intRow = intRow + 1
strISO = ThisWorkbook.Sheets("MS_Data").Range("B" & intRow).Text

Wend MsgBox "Done"

End Sub



I would really appreciate any help.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi cynthixie
try this code
VBA Code:
Sub Mail()

Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(oMailItem)
Dim strMailBody As String
Dim Cel    As Range

For Each Cel In Sheets("MS_Data").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)

    If Cel.Offset(0, 7).Value = "Y" Then ' "Y" or "y" - Case sensitive
    
Set objEmail = objOutlook.CreateItem(oMailItem)

StrMailSubject = ThisWorkbook.Sheets("Mail_Details").Range("A2").Text
strMailBody = "<BODY style='font-size:11pt;font-family:Calibri(Body)'>" & ThisWorkbook.Sheets("Mail_Details").Range("B2").Text & "</BODY>"
strMailBody = Replace(strMailBody, Chr(10), "<br>")

strFolder = "C:\Users\CIOTTIC\OneDrive - IAEA\Desktop\AL TEST"

strISO = Cel.Offset(0, 1).Value
strSalutation = Cel.Offset(0, 2).Value
strEmail = Cel.Offset(0, 3).Value
strCC = Cel.Offset(0, 4).Value
strFile = Cel.Offset(0, 5).Value
strFile2 = Cel.Offset(0, 6).Value
'
StrMailSubject = Replace(StrMailSubject, "<ISO>", strISO)
strMailBody = Replace(strMailBody, "<Salutation>", strSalutation)

        With objEmail
            .to = CStr(strEmail)
            .CC = CStr(strCC)
            .Subject = StrMailSubject
            .BodyFormat = olFormatHTML
            .display
            .Attachments.Add strFolder & "\" & strFile
            .Attachments.Add strFolder & "\" & strFile2
            .HTMLBody = strMailBody & .HTMLBody
            .Send
        End With

    End If
Next Cel

MsgBox "Done"

End Sub
 
Upvote 0
Solution
Hi cynthixie
try this code
VBA Code:
Sub Mail()

Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(oMailItem)
Dim strMailBody As String
Dim Cel    As Range

For Each Cel In Sheets("MS_Data").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)

    If Cel.Offset(0, 7).Value = "Y" Then ' "Y" or "y" - Case sensitive
   
Set objEmail = objOutlook.CreateItem(oMailItem)

StrMailSubject = ThisWorkbook.Sheets("Mail_Details").Range("A2").Text
strMailBody = "<BODY style='font-size:11pt;font-family:Calibri(Body)'>" & ThisWorkbook.Sheets("Mail_Details").Range("B2").Text & "</BODY>"
strMailBody = Replace(strMailBody, Chr(10), "<br>")

strFolder = "C:\Users\CIOTTIC\OneDrive - IAEA\Desktop\AL TEST"

strISO = Cel.Offset(0, 1).Value
strSalutation = Cel.Offset(0, 2).Value
strEmail = Cel.Offset(0, 3).Value
strCC = Cel.Offset(0, 4).Value
strFile = Cel.Offset(0, 5).Value
strFile2 = Cel.Offset(0, 6).Value
'
StrMailSubject = Replace(StrMailSubject, "<ISO>", strISO)
strMailBody = Replace(strMailBody, "<Salutation>", strSalutation)

        With objEmail
            .to = CStr(strEmail)
            .CC = CStr(strCC)
            .Subject = StrMailSubject
            .BodyFormat = olFormatHTML
            .display
            .Attachments.Add strFolder & "\" & strFile
            .Attachments.Add strFolder & "\" & strFile2
            .HTMLBody = strMailBody & .HTMLBody
            .Send
        End With

    End If
Next Cel

MsgBox "Done"

End Sub
omg Sequoyah THANK YOU SO MUCH! It worked perfectly.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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