My sheet 'Volglijst' contains a list with all packages that have been registerd for sending.
When the package is picked up by the supplier or courrier, the goods reception service registers the date and who picked up the package.
When they close the file, a pop-up appears that is asking if they want to send e-mail confirmation to the person who requested the sending.
When they select yes VBA should check all rows in sheet 'Volglijst' where there is a date in Column B and Column Q, and where column S is empty (the 3 conditions should apply at the same time, if not, no e-mail needs to be send).
I'm getting my outlook to start and create a new e-mail, but it remains empty.
The body is working for an other e-mail only the reference to the cell content is adjusted to match the rows for which the conditions apply.
Dim OutApp As Object
Dim OutMail As Object
Dim i As Long
Dim t As Range
Dim WkSht As Worksheet
Dim strbody As String
Set WkSht = Sheets("Volglijst")
For i = 1 To 999
If WkSht.Cells(i, 2).Value <> "" And WkSht.Cells(i, 17).Value <> "" And WkSht.Cells(i, 19).Value = "" Then
Dim rng As Range
With Application.Intersect(WkSht.Rows(i), WkSht.UsedRange)
Set rng = WkSht.Range(WkSht.Cells(i, 3), .Cells(.Cells.Count))
End With
If rng Is Nothing Then
Exit Sub
End If
End If
Next
On Error Resume Next
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<font size=""3"" face=""Calibri"">" & _
"Beste Collega,<br><br>" & _
"Uw pakket met nummer <B>" & WkSht.Cells(WkSht.Rows(i), 1).Value & "</B> werd <B>" & WkSht.Cells(WkSht.Rows(i), 17).Value & "</B> opgehaald door <B>" & WkSht.Cells(WkSht.Rows(i), 16).Value & "</B>.<br>" & _
"Bijkomende opmerkingen goederenontvangst: <B>" & WS.Cells(WkSht.Rows(i), 18).Value & "</B>.<br>" & _
"<br><br>In geval van vragen gelieve contact op te nemen." & _
"<br><br> Met vriendelijke groeten, </font>"
On Error Resume Next
With OutMail
.To = WS.Cells(WkSht.Rows(i), 5).Value
.CC = ""
.BCC = ""
.Subject = "Ophaling pakket " & WS.Cells(i, 1).Value & ""
.HTMLBody = strbody
.Display 'or use .Send
End With
A separate email sould be sent for each row with column B <> ""; column Q <> "", Column S = "" , and the recepient is the e-mail adres of column E in that row. Details in the email body should also come from the applicable row.
Will attempt to compartmentalize this, but you have a couple significant issue:
You utilize i in the outlook aspect of this email OUTSIDE of the loop, so you're just using i = 999, or was this intended to be within the loop?
The references in your strbody were to different worksheets... check your references. I called out the first occurrence in the Excel_Activities subroutine when I started defining each use.
Public bdy_a as string, bdy_b as string, bdy_c as string, bdy_d as string
Public to_ as string
Public sub_ as string
'
Sub Excel_Activities()
Dim i as Long, t As Range
Dim WkSht As Worksheet
Dim strbody As String
Set WkSht = Sheets("Volglijst")
For i = 1 To 999
If WkSht.Cells(i, 2).Value <> "" And WkSht.Cells(i, 17).Value <> "" And WkSht.Cells(i, 19).Value = "" Then
Dim rng As Range
With Application.Intersect(WkSht.Rows(i), WkSht.UsedRange)
Set rng = WkSht.Range(WkSht.Cells(i, 3), .Cells(.Cells.Count))
End With
If rng Is Nothing Then
Exit Sub
Else
bdy_a = WkSht.Cells(i, 1).Value
bdy_b = WkSht.Cells(i, 17).Value
bdy_c = WkSht.Cells(i, 16).Value
bdy_d = WS.Cells(i, 18).Value 'IS THIS CORRECT SHEET?
to_ = WS.Cells(WkSht.Rows(i), 5).Value
sub_ = WS.Cells(i, 1).Value
Application.Run("Outlook_Activities")
End If
End If
Next
End Sub
Then deal with the saved values to create the desired email, which appears to occur within the Loop based on the use of i in your original post strbody.
Private Sub Outlook_Activities()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<font size=""3"" face=""Calibri"">" & "Beste Collega,<br><br>" & "Uw pakket met nummer <B>" & bdy_a & "</B> werd <B>" & bdy_b & "</B> opgehaald door <B>" & bdy_c & "</B>.<br>" & "Bijkomende opmerkingen goederenontvangst: <B>" & & "</B>.<br>" & "<br><br>In geval van vragen gelieve contact op te nemen." & "<br><br> Met vriendelijke groeten, </font>"
With OutMail
.To = to_
.CC = ""
.BCC = ""
.Subject = "Ophaling pakket " & sub_ & ""
.HTMLBody = strbody
.Display 'or use .Send
End With
End Sub
I believe the following is what you are looking for, this will loop from Row 1 to the last in the UsedRange, check whether Columns B & Q are not empty and Column S is empty then deal with the email per row:
Sub LoopThroughRange_SendEmail()
Dim OutApp As Object: Set OutApp = CreateObject("Outlook.Application")
Dim OutMail As Object: Set OutMail = OutApp.CreateItem(0)
Dim i As Long
Dim strbody As String
Dim WkSht As Worksheet: Set WkSht = Sheets("Volglijst")
For i = 1 To WkSht.UsedRange.Rows.Count
If WkSht.Cells(i, "B").Value <> "" And WkSht.Cells(i, "Q").Value <> "" And WkSht.Cells(i, "S").Value = "" Then
strbody = "<html><body><font size=""3"" face=""Calibri"">Beste Collega,<br><br>Uw pakket met nummer <b>" & _
WkSht.Cells(i, "A").Value & "</b> werd <b>" & WkSht.Cells(i, "Q").Value & "</b> opgehaald door <b>" & _
WkSht.Cells(i, "P").Value & "</b>.<br>Bijkomende opmerkingen goederenontvangst: <b>" & _
WkSht.Cells(i, "R").Value & "</B>.<br><br><br>In geval van vragen gelieve contact op te nemen.<br><br>" & _
"Met vriendelijke groeten, </font></body></html>"
With OutMail
.To = WkSht.Cells(i, "E").Value
.CC = ""
.BCC = ""
.Subject = "Ophaling pakket " & WkSht.Cells(i, "A").Value & ""
.HTMLBody = strbody
.Display 'or use .Send
End With
End If
Next i
End Sub
UPDATE:
To also validate the email address before attempting to send the email, the below will help, it will allow multiple email addresses in a single cells separated by a ;
Sub LoopThroughRange_SendEmail()
Dim oRegEx As Object
Set oRegEx = CreateObject("VBScript.RegExp")
Dim OutApp As Object: Set OutApp = CreateObject("Outlook.Application")
Dim OutMail As Object: Set OutMail = OutApp.CreateItem(0)
Dim i As Long
Dim strbody As String
Dim WkSht As Worksheet: Set WkSht = Sheets("Volglijst")
For i = 1 To WkSht.UsedRange.Rows.Count
If ValidEmail(WkSht.Cells(i, "E").Value, oRegEx) Then
If WkSht.Cells(i, "B").Value <> "" And WkSht.Cells(i, "Q").Value <> "" And WkSht.Cells(i, "S").Value = "" Then
strbody = "<html><body><font size=""3"" face=""Calibri"">Beste Collega,<br><br>Uw pakket met nummer <b>" & _
WkSht.Cells(i, "A").Value & "</b> werd <b>" & WkSht.Cells(i, "Q").Value & "</b> opgehaald door <b>" & _
WkSht.Cells(i, "P").Value & "</b>.<br>Bijkomende opmerkingen goederenontvangst: <b>" & _
WkSht.Cells(i, "R").Value & "</B>.<br><br><br>In geval van vragen gelieve contact op te nemen.<br><br>" & _
"Met vriendelijke groeten, </font></body></html>"
With OutMail
.To = WkSht.Cells(i, "E").Value
.CC = ""
.BCC = ""
.Subject = "Ophaling pakket " & WkSht.Cells(i, "A").Value & ""
.HTMLBody = strbody
.Display 'or use .Send
End With
End If
Else
'email address is not valid
End If
Next i
End Sub
Public Function ValidEmail(pAddress As String, ByRef oRegEx As Object) As Boolean
With oRegEx
.Pattern = "^(([a-zA-Z0-9_\-\.\']+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)(\s*;\s*|\s*$))+$" 'pattern for multiple email addresses included
ValidEmail = .test(pAddress)
End With
End Function
Related
I have three columns: A) Enterprises B) Email address matching the enterprise C) Yes or No
If there is a YES in column C, I want to send a message to the email address in column B.
This is what I have. Nothing is happening.
Sub Test2()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" And _
LCase(Cells(cell.Row, "C").Value) = "yes" _
And LCase(Cells(cell.Row, "D").Value) <> "send" Then
Set OutMail = OutApp.CreateItem(olMailItem)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Reminder"
.Body = "Dear " & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"Please contact us to discuss bringing " & _
"your account up to date."
Attachments.Add ("\\C:\test.pdf")
.Send '
End With
On Error GoTo 0
Cells(cell.Row, "D").Value = "send"
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
The code below will loop through Row 2 to the last row in the UsedRange and make sure that Columns A, B & C are not empty as well as check to make sure Column D is empty, which the code uses as a flag to show whether the email has previously been sent.
I've added a Regex validation function to the code to validate the email address.
Sub LoopThroughRange_SendEmail()
Dim OutApp As Object: Set OutApp = CreateObject("Outlook.Application")
Dim OutMail As Object: Set OutMail = OutApp.CreateItem(0)
Dim oRegEx As Object
Set oRegEx = CreateObject("VBScript.RegExp")
Dim i As Long
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
'declare and set the worksheet you are working with
For i = 2 To ws.UsedRange.Rows.Count
'loop from Row 2 To Last Row in UsedRange
If ws.Cells(i, "A").Value <> "" And ws.Cells(i, "B").Value <> "" And ws.Cells(i, "C").Value = "Yes" And ws.Cells(i, "D").Value = "" Then
' make sure that Columns A, B & C are not empty and D is empty (which we will use as a flag to show that the email did get sent.
If ValidEmail(ws.Cells(i, "B").Value, oRegEx) Then
With OutMail
.To = ws.Cells(i, "B").Value
.CC = ""
.BCC = ""
.Subject = "Reminder"
.Body = "Dear " & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"Please contact us to discuss bringing " & _
"your account up to date."
.Attachments.Add ("\\C:\test.pdf")
.Display '.Send
End With
ws.Cells(i, "D").Value = "Sent # " & Format(Now(), "yyyy-MM-dd hh:mm:ss")
Else
ws.Cells(i, "D").Value = "Email not valid"
End If
End If
End Sub
Public Function ValidEmail(pAddress As String, ByRef oRegEx As Object) As Boolean
With oRegEx
.Pattern = "^(([a-zA-Z0-9_\-\.\']+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)(\s*;\s*|\s*$))+$" 'pattern for multiple email addresses included
ValidEmail = .test(pAddress)
End With
End Function
My code is sending an e-mail through Outlook.
I want the value of two cells included in the e-mail body.
Formatting is working and the text is properly shown.
Also the cell value is shown properly in one of two cases.
For one part of the code, it is adding “ACCT:ACCT:ACCT:” in front of the cell value.
The cell value for cell (6,3) = Zeer Dringend
The cell value for cell (22,3) = 2019-0004
This is the e-mail body generated by the code
Beste Collega,
Een nieuwe retour zending registratie werd aangemaakt met urgentie: **ACCT:ACCT:ACCT:Zeer Dringend**.
Het pakket nummer is **2019-0004**.
In geval van vragen gelieve contact op te nemen.
Met vriendelijke groeten,
Where it shows ACCT:ACCT:ACCT:Zeer Dringend, it should say Zeer Dringend
This is the full code
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim WS As Worksheet
If ActiveWorkbook.Path <> "" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set WS = Sheets("Ingave")
strbody = "<font size=""3"" face=""Calibri"">" & _
"Beste Collega,<br><br>" & _
"Een nieuwe retour zending registratie werd aangemaakt met urgentie: <B>" & WS.Cells(6, 3).Value & "</B>.<br>" & _
"Het pakket nummer is <B>" & WS.Cells(22, 3).Value & "</B>.<br><B> " & _
"</B><br><br>In geval van vragen gelieve contact op te nemen." & _
"<br><br> Met vriendelijke groeten, </font>"
On Error Resume Next
With OutMail
.to = "xxx#yyy"
.CC = ""
.BCC = ""
.Subject = "Nieuwe registratie retour pakket "
.HTMLBody = strbody
.Display 'or use .Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Else
MsgBox "U moet de file eerst opslaan voor u verder kan gaan."
End If
The code you provided seems to work as expected, you should attempt to investigate where the ACCT:ACCT:ACCT: string is actually coming from, maybe the following helps, where I've changed the ActiveWorkbook to ThisWorkbook to eliminate the possibility of the code picking up data from a workbook that might be active, but not necessarily the intended one:
Sub Send_Email()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Ingave")
'make sure that we are looking at the this workbook and not the Active one.
If ThisWorkbook.Path <> "" Then
'make sure the workbook has been saved, so we get a Path property.
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<font size=""3"" face=""Calibri"">" & _
"Beste Collega,<br><br>" & _
"Een nieuwe retour zending registratie werd aangemaakt met urgentie: <B>" & ws.Cells(6, 3).Value & "</B>.<br>" & _
"Het pakket nummer is <B>" & ws.Cells(22, 3).Value & "</B>.<br><B> " & _
"</B><br><br>In geval van vragen gelieve contact op te nemen." & _
"<br><br> Met vriendelijke groeten, </font>"
'On Error Resume Next
With OutMail
.to = "xxx#yyy"
.CC = ""
.BCC = ""
.Subject = "Nieuwe registratie retour pakket "
.HTMLBody = strbody
.Display 'or use .Send
End With
'On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Else
MsgBox "U moet de file eerst opslaan voor u verder kan gaan."
End If
End Sub
I need to send an email with Excel with two conditions.
Cell D2 must be >= 1
Cell E2 must be ="".
I've the first condition done, but not the second one
The code is:
'PRAZO Etapa 4
Public Sub EnviarEmailEt4()
Dim OutApp As Object
Dim OutMail As Object
Dim Body As String
Worksheets("Incidentes2019").Select
Range("D4").Select
Do While ActiveCell.Value <> ""
If ActiveCell >= 1 And ActiveCell.Offset(0, 1) = "" And InStr(2, Cells(ActiveCell.Row, 10), "#") > 0 Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = Cells(ActiveCell.Row, 10).Value
.CC = Cells(ActiveCell.Row, 11).Value
.BCC = ""
.Subject = Cells(ActiveCell.Row, 3).Value
If (ActiveCell = 1) Or (ActiveCell = 2) Then
.Body = "ALERTA PRAZO ETAPA 4!!" & vbNewLine & vbNewLine & "Nº GQE " & Cells(ActiveCell.Row, 2).Value & " - " & Cells(ActiveCell.Row, 3).Value
ElseIf (ActiveCell >= 3) Then
.Body = "ULTRAPASSADO PRAZO ETAPA 4!!" & vbNewLine & vbNewLine & "Nº GQE " & Cells(ActiveCell.Row, 2).Value & " - " & Cells(ActiveCell.Row, 3).Value
End If
.Send 'Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
MsgBox "Alerta Etapa 4 enviado - " & Format(Now, "HH:MM") & vbNewLine & Cells(ActiveCell.Row, 3).Value
End If
Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
Loop
End Sub
Try this, you can check in a loop outside the mail procedure if the cells meet your criteria, if so then you send the mail:
Option Explicit
Sub SendingMails()
Dim ws As Worksheet 'always declare worksheets and workbooks to avoid using select
Dim SendTo As String, SendSubject As String, FirstData As String, SecondData As String 'here, variables for the items to fill on your mail
Dim LastRow As Long, i As Long 'Here you have the lastrow of the worksheet and another variable for a loop
Set ws = ThisWorkbook.Sheets("Sheet1") 'Change Sheet1 for the name of the sheet where you keep the data
With ws
LastRow = .Cells(.Rows.Count, 4).End(xlUp).Row 'this will check the last row with data on the column 4 (D)
For i = 2 To LastRow 'starting from row 2 to the last one with data
If .Cells(i, 4) >= 1 And .Cells(i, 5) <> vbNullString Then 'here you check if column D cell has 1 or higher and if column E cell is empty
SendTo = .Cells(i, 10)
SendSubject = .Cells(i, 3)
FirstData = .Cells(i, 2)
SecondData = .Cells(i, 3)
Call EnviarEmailEt4(SendTo, SendSubject, FirstData, SecondData)
End If
Next i
End With
End Sub
Sub EnviarEmailEt4(SendTo As String, SendSubject As String, FirstData As String, SecondData As String)
'as you can see above, i've declared variables inside the procedure which will be taken from the previous one
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = SendTo
.CC = ""
.BCC = ""
.Subject = SendSubject
.Body = "ALERTA FIM DE PRAZO ETAPA 4!!" & vbNewLine & vbNewLine & "Nº GQE " & FirstData & " - " & SecondData
'.Attachments.Add ActiveWorkbook.FullName 'Anexar este ficheiro
'.Attachments.Add ("") 'Anexar outro ficheiro
.send 'Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
MsgBox " Alerta Et4 enviado - " & Format(Now, "HH:MM") 'I Would avoid alerting in each loop if there are lots of mails
End Sub
You can select cell on the right (like pressing the arrow in excel) using Range.offset() property. Try to change your IF statement to the following:
If ActiveCell >= 1 And ActiveCell.Offset(0, 1) <> "" Then
EDIT:
in response to the change in your question: here is a working approach to set mailbody based on the activecell value:
If (ActiveCell = 1) Or (ActiveCell = 2) Then
MailBody = "ALERTA PRAZO ETAPA 4!!" & vbNewLine & vbNewLine & "Nº GQE " & Cells(ActiveCell.Row, 2).Value & " - " & Cells(ActiveCell.Row, 3).Value
ElseIf (ActiveCell >= 3) Then
MailBody = "ULTRAPASSADO PRAZO ETAPA 4!!" & vbNewLine & vbNewLine & "Nº GQE " & Cells(ActiveCell.Row, 2).Value & " - " & Cells(ActiveCell.Row, 3).Value
End If
I have a vba code which generates a outlook email, populates with required To, CC, Subject and Body when i change a particular column in excel. And when the email is sent my status column updates to 'Closed' and Email Sent Flag column updates to '1'.
But the problem is when i click on close instes on Send on my email( which was generated and auto populated) even then my status and Email sent flag column gets updated with Closed and 1 respectively. Below is my code.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xOutApp As Object
Dim xMailItem As Object
Dim xMailBody As String
Dim html As String
Dim intR As String
Dim ccStr As String
Dim Signature As String
Dim html1 As String
'Dim itmevt As New CMailItemEvents
'Dim tsp As String
lRow = Cells(Rows.Count, 17).End(xlUp).Row
lRow1 = ThisWorkbook.Sheets("Validation Lists").Cells(Rows.Count, 4).End(xlUp).Row
html = "<br>" & ("Hi,") & ("Please spare some time to provide feedback for our service. This will help us to serve you better for upcoming services.") & "<br>"
For i = 2 To lRow1
ccStr = ";" & ThisWorkbook.Sheets("Validation Lists").Cells(i, "D").Value & ccStr
Next i
For i = 1 To lRow
If (Cells(i, "Q").Value = "Closed") And (Cells(i, "R").Value <> "1") Then
intR = MsgBox("Do you want to send a feedback for " & Cells(i, "B") & "Viz." & Cells(i, "C").Value & " to " & Cells(i, "C") & "?", vbQuestion + vbYesNo)
If intR = vbYes Then
Set xOutApp = CreateObject("Outlook.Application")
Set xMailItem = xOutApp.CreateItem(0)
With xMailItem
.To = Cells(i, "I").Value
.CC = ccStr
.display
Signature = .HTMLBody
.Subject = "Feedback for " & Cells(i, "B").Value & " viz. " & Cells(i, "C").Value
.HTMLBody = html & "This request was assited by " & Cells(i, "K").Value & "<br><br>" & Signature
'.dispaly
'.Send
End With
Cells(i, "R").Value = "1"
Set xRgSel = Nothing
Set xOutApp = Nothing
Set xMailItem = Nothing
On Error Resume Next
End If
If intR = vbNo Then Cells(i, "Q").Value = "In Progress"
End If
Next i
End Sub
You have to check if the message has been sent.
There exists a boolean message property named Sent.
Untested but could work:
Loop until .Sent is True.
With xMailItem
.To = Cells(i, "I").Value
.CC = ccStr
.display
Signature = .HTMLBody
.Subject = "Feedback for " & Cells(i, "B").Value & " viz. " & Cells(i, "C").Value
.HTMLBody = html & "This request was assited by " & Cells(i, "K").Value & "<br><br>" & Signature
Do Until .Sent = True
DoEvents
Loop
End With
I have a slight issue with a macro. It works fine at the moment, but I need to add some code to do the following but don't know at what point to add it:
If for each cell in Column C that there is a blank cell to look for the email address on the same row but 10 columns over to the right in Column M
In the start of the body "Hi There (Column B content)
In the body of the email I would like for the macro to insert the contents from column F like this: "Please choose the following option (Column F content)
Any Ideas on how I can modify the code to include these options please.
Thank you for your time.
Sub Send_Email()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim cel As Range
Dim SigString As String
Dim Signature As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
SigString = Environ("appdata") & _
"\Microsoft\Signatures\GBS.txt"
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
For Each cel In Range(("C2"), Range("C2").End(xlDown))
strbody = "Hi there" & vbNewLine & vbNewLine & _
"My name Is William, Please choose the following option ..." & vbNewLine & _
"I work at Fair" & vbNewLine & _
"Bye" & vbNewLine & _
"WH"
On Error Resume Next
With OutMail
.To = cel.Value
.CC = cel.Offset(0, 10).Value
'.BCC = ""
.Subject = "Choose you plan"
.Body = strbody & vbNewLine & vbNewLine & Signature
.Display
'.Attachments.Add ("C:\test.txt")
'.Send
End With
On Error GoTo 0
Next cel
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Try this one:
Sub Send_Email()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim cel As Range
Dim SigString As String
Dim Signature As String
Dim lastrow As Long
Set OutApp = CreateObject("Outlook.Application")
SigString = Environ("appdata") & _
"\Microsoft\Signatures\GBS.txt"
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
For Each cel In Range("C2:C" & lastrow)
strbody = "Hi there" & cel.Offset(, -1) & vbNewLine & vbNewLine & _
"My name Is William, Please choose the following option ..." & vbNewLine & _
cel.Offset(, 3) & _
"I work at Fair" & vbNewLine & _
"Bye" & vbNewLine & _
"WH"
On Error Resume Next
With OutApp.CreateItem(0)
If cel.Value <> "" Then
.To = cel.Value
.CC = cel.Offset(0, 10).Value
Else
.To = cel.Offset(0, 10).Value & ", " & Join(Application.Index(cel.Offset(, -2).Resize(, 4).Value, 0), ", ")
End If
'.BCC = ""
.Subject = "Choose you plan"
.Body = strbody & vbNewLine & vbNewLine & Signature
.Display
'.Attachments.Add ("C:\test.txt")
'.Send
End With
On Error GoTo 0
Next cel
Set OutApp = Nothing
End Sub