I am trying to make a macro that loops through a table on any sheet across a document and generated emails with info from that table to the individuals on each line. I think I got most of it to work but I can't figure out why .Body is no longer sending any information to outlook.
When I had shorter test messages it worked but now it isn't sending any body text to Outlook. My .To line is working fine as is my subject.
With OutMail
.To = Range("D" & i).Value
.CC = ""
.BCC = ""
.Subject = ActiveSheet.Name & " Service Insert"
'.HTMLBody = "Hi " & Range(C, i).Value & "<br><br>" & "Please see your Service Insert Below." & "<br>" & "<br>" _
& Range(S, 1).Value & "<br>" & "Services Played: " & Range(S, i).Value & "<br>" & "Doubling Services: " & Range("T" & i).Value & "<br>" & "Move Up Services: " & Range("W" & i).Value & " services from " & Range(Y, i) & "<br>" & "Solo Services: " & Range(Z, i).Value & "<br><br>" _
& Range(AA, 1).Value & "<br>" & "Services Played: " & Range(AA, i).Value & "<br>" & "Doubling Services: " & Range(AB, i).Value & "<br>" & "Move Up Services: " & Range(AE, i).Value & " services from " & Range(AG, i) & "<br>" & "Solo Services: " & Range(AH, i).Value & "<br><br>" _
& "Pay Period Totals" & "<br>" & "Total Leave Used: " & Range(F, i).Value & "<br>" & "Sick Leave Used: " & Range(i, i).Value & "<br>" & "Total Doubling Pay: " & Range(K, i).Value & "<br>" & "Total Move Up Pay: " & Range(L, i).Value & "<br>" & "Total Solo Pay: " & Range(M, i).Value & "<br>" & "Total Pay Correction: " & Range(N, i).Value & "<br>" & "Parking Reimbursement: " & Range(O, i).Value & "<br>" & "Mileage Reimbursement: " & Range(P, i).Value & "<br>" & "Travel Reimbursement: " & Range(Q, i).Value & "<br>" & "Total Additional Pay: " & Range(R, i).Value & "<br><br>" _
& "Season Totals" & "<br><br>" & "Total Season Services Used: " & Range(AZ, i).Value & "<br>" & "Sick Leave Remaining: " & Range(AY, i).Value & "<br><br>" & "Please let me know if you have any questions or concerns." & "<br><br>" & "Best, "
.Body = "Hi " & Range("C" & i).Value & vbNewLine & vbNewLine & "Please see your Service Insert Below." & vbNewLine & vbNewLine _
& Range("S1").Value & vbNewLine & "Services Played: " & Range("S" & i).Value & vbNewLine & "Doubling Services: " & Range("T" & i).Value & vbNewLine & "Move Up Services: " & Range("W" & i).Value & " services from " & Range("Y" & i) & vbNewLine & "Solo Services: " & Range("Z" & i).Value & vbNewLine & vbNewLine _
& Range("AA1").Value & vbNewLine & "Services Played: " & Range("AA" & i).Value & vbNewLine & "Doubling Services: " & Range("AB" & i).Value & vbNewLine & "Move Up Services: " & Range("AE" & i).Value & " services from " & Range("AG" & i) & vbNewLine & "Solo Services: " & Range("AH" & i).Value & vbNewLine & vbNewLine _
& "Pay Period Totals" & vbNewLine & "Total Leave Used: " & Range("F" & i).Value & vbNewLine & "Sick Leave Used: " & Range("I" & i).Value & vbNewLine & "Total Doubling Pay: " & Range("K" And i).Value & vbNewLine & "Total Move Up Pay: " & Range("L" And i).Value & vbNewLine & "Total Solo Pay: " & Range("M" And i).Value & vbNewLine & "Total Pay Correction: " & Range("N" And i).Value & vbNewLine & "Parking Reimbursement: " & Range("O" And i).Value & vbNewLine & "Mileage Reimbursement: " & Range("P" And i).Value & vbNewLine & "Travel Reimbursement: " & Range("Q" And i).Value & vbNewLine & "Total Additional Pay: " & Range("R" And i).Value & vbNewLine & vbNewLine _
& "Season Totals" & vbNewLine & vbNewLine & "Total Season Services Used: " & Range("AZ" & i).Value & vbNewLine & "Sick Leave Remaining: " & Range("AY" & i).Value & vbNewLine & vbNewLine & "Please let me know if you have any questions or concerns." & vbNewLine & vbNewLine & "Best, "
'.Attachments.Add ActiveWorkbook.FullName
.Display 'or use .Send
End With
On Error GoTo 0
This is the Body and HTMLBody I tried to make work, neither is sending any information, even failed cell searches to outlook.
I can show more of my the code if needed, for readability I hope this is enough.
Create a variable to create your body string by making some concatenations then assign the string to .Body. This way you can easily check your string and find an error by running step by step.
Dim emailBody as string: emailBody = ""
emailBody = emailBody & "Hi " & Range("C" & i).Value & vbNewLine & vbNewLine
emailBody = emailBody & "Please see your Service Insert Below." & vbNewLine & vbNewLine
emailBody = emailBody & Range("S1").Value & vbNewLine
...
With OutMail
...
.Body = emailBody
...
End With
Related
I've spent the last couple few weeks to try to figure this out why below goven is not working, I've managed to stop all the errors, however the email doesn't show up in my inbox . I've tried to change everything up and still it doesn't show up. The main purpose is to send the data with his or her data only to an gmail,
Sub send_email_via_Gmail()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
Dim myMail As CDO.Message
Set myMail = New CDO.Message
Dim i As Integer
Dim j As Integer
Dim last_row As Integer
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smptauthenticate") = 1
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 1465
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "shiva.nand#kaercher.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "123456"
last_row = Application.WorksheetFunction.CountA(sh.Range("X:X"))
For i = 2 To last_row
myMail.To = sh.Range("Y" & i).Value
myMail.CC = sh.Range("Z" & i).Value & " ; " & sh.Range("AA" & i).Value & " ; " & sh.Range("AB" & i).Value
myMail.Subject = "Order Dispatch Details"
myMail.htmlbody = "<html><body><br>Dear Concern, Please find below dispatch status<br><br><style>table, th, td { border: 1px solid black; border-collapse: collapse;}</style>" & _
"<table><tr><td><b>" & sh.Range("A1").Value & "</b></td><td><b>" & sh.Range("B1").Value & "</b></td><td><b>" & sh.Range("C1").Value & "</b></td><td><b>" & sh.Range("D1").Value & "</b></td><td><b>" & sh.Range("E1").Value & "</b></td><td><b>" & sh.Range("F1").Value & "</b></td><td><b>" & sh.Range("G1").Value & "</b></td><td><b>" & sh.Range("H1").Value & "</b></td><td><b>" & sh.Range("I1").Value & "</b></td><td><b>" & sh.Range("J1").Value & "</b></td><td><b>" & sh.Range("K1").Value & "</b></td><td><b>" & sh.Range("L1").Value & "</b></td><td><b>" & sh.Range("M1").Value & "</b></td><td><b>" & sh.Range("N1").Value & "</b></td><td><b>" & sh.Range("O1").Value & "</b></td><td><b>" & sh.Range("P1").Value & "</b></td><td><b>" & sh.Range("Q1").Value & "</b></td><td><b>" & sh.Range("R1").Value & "</b></td><td><b>" & sh.Range("S1").Value & "</b></td><td><b>" & sh.Range("T1").Value & "</b></td><td><b>" & sh.Range("U1").Value & "</b></td><td><b>" & sh.Range("V1").Value & _
"</b></td><td><b>" & sh.Range("W1").Value & "</b></td><td><b>" & sh.Range("X1").Value & _
"<tr><td>" & sh.Range("A" & i).Value & "</td><td>" & sh.Range("B" & i).Value & "</td><td>" & sh.Range("C" & i).Value & "</td><td>" & sh.Range("D" & i).Value & "</td><td>" & sh.Range("E" & i).Value & "</td><td>" & sh.Range("F" & i).Value & "</td><td>" & sh.Range("G" & i).Value & "</td><td>" & sh.Range("H" & i).Value & "</td><td>" & sh.Range("I" & i).Value & "</td><td>" & sh.Range("J" & i).Value & "</td><td>" & sh.Range("K" & i).Value & "</td><td>" & sh.Range("L" & i).Value & "</td><td>" & sh.Range("M" & i).Value & "</td><td>" & sh.Range("N" & i).Value & "</td><td>" & sh.Range("O" & i).Value & "</td><td>" & sh.Range("P" & i).Value & "</td><td>" & sh.Range("Q" & i).Value & "</td><td>" & sh.Range("R" & i).Value & "</td><td>" & sh.Range("S" & i).Value & "</td><td>" & sh.Range("T" & i).Value & "</td><td>" & sh.Range("U" & i).Value & "</td><td>" & sh.Range("V" & i).Value & "</td><td>" & sh.Range("W" & i).Value & "</td><td>" & sh.Range("X" & i).Value & "</td><td>"
For j = (i + 1) To last_row
If sh.Range("Y" & i).Value = sh.Range("Y" & j).Value Then
myMail.htmlbody = myMail.htmlbody & "<tr><td>" & sh.Range("A" & j).Value & "</td><td>" & sh.Range("B" & j).Value & "</td><td>" & sh.Range("C" & j).Value & "</td><td>" & sh.Range("D" & j).Value & "</td><td>" & sh.Range("E" & j).Value & "</td><td>" & sh.Range("F" & j).Value & "</td><td>" & sh.Range("G" & j).Value & "</td><td>" & sh.Range("H" & j).Value & "</td><td>" & sh.Range("I" & j).Value & "</td><td>" & sh.Range("J" & j).Value & "</td><td>" & sh.Range("K" & j).Value & "</td><td>" & sh.Range("L" & j).Value & "</td><td>" & sh.Range("M" & j).Value & "</td><td>" & sh.Range("N" & j).Value & "</td><td>" & sh.Range("O" & j).Value & "</td><td>" & sh.Range("P" & j).Value & "</td><td>" & sh.Range("Q" & j).Value & "</td><td>" & sh.Range("R" & j).Value & "</td><td>" & sh.Range("S" & j).Value & "</td><td>" & sh.Range("T" & j).Value & "</td><td>" & sh.Range("U" & j).Value & "</td><td>" & sh.Range("V" & j).Value & "</td><td>" & sh.Range("W" & j).Value & "</td><td>" & sh.Range("X" & j).Value & "</td><td>"
i = j
End If
Next j
myMail.htmlbody = myMail.htmlbody & "</table></body><br>Regards,<br>Vikas Karn,<br> Mobile no: +91-83 7795 2790</html>"
myMail.Send
Next i
MsgBox "Sent"
Set myMail = Nothing
End Sub
The last line is causing some trouble.
MsgBox ("Book Code = " & ISBN & vbNewLine & _
"Book Title= " & title & vbNewLine & _
"Unit Price= " & title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & (Format(CustomerInput * UnitCost), Currency))
Move the close-parenthesis after UnitCost in the Format statement to the end, and Currency in the format statement needs to be a string (i.e: in quotes):
MsgBox ("Book Code = " & ISBN & vbNewLine & _
"Book Title= " & Title & vbNewLine & _
"Unit Price= " & Title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & (Format(CustomerInput * UnitCost, "Currency")))
Although the above works, you can omit some of your parentheses, and possibly make the code a bit easier to understand:
MsgBox "Book Code = " & ISBN & vbNewLine & _
"Book Title= " & Title & vbNewLine & _
"Unit Price= " & Title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & Format(CustomerInput * UnitCost, "Currency")
I have in "Sheet1" numerous email addresses, in columns K, M, O, Q, S, U, W, Y, AA.
I want to create an email that will be sent to all the addresses taken from the last row in Sheet1. Same for data in email body taken from last row.
Dim MonOutlook As Object
Dim MonMessage As Object
Dim EmailTo As String
With Worksheets("Sheet1")
EmailTo = .Range("K" & ligne) & ";" & .Range("M" & ligne) & ";" & .Range("O" & ligne) & ";" & .Range("Q" & ligne) & ";" & .Range("S" & ligne) & ";" & .Range("U" & ligne) & ";" & .Range("W" & ligne) & ";" & .Range("Y" & ligne) & ";" & .Range("AA" & ligne)
End With
Set MonOutlook = CreateObject("Outlook.Application")
Set MonMessage = MonOutlook.CreateItem(0)
MonMessage.To = ""
MonMessage.Cc = ""
MonMessage.Bcc = EmailTo
MonMessage.Subject = "Rate request" & " " & "for" & " " & ThisWorkbook.Sheets("Sheet1").Range("B" & ligne)
MonMessage.body = "Hello,"
Chr (13) & Chr(13) & "Please send me rate for" & " " & ThisWorkbook.Sheets("Sheet1").Range("G" & ligne) & " " & "rooms on basis" & " " & ThisWorkbook.Sheets("Sheet1").Range("H" & ligne) & _
Chr(13) & Chr(13) & "in hotel:" & " " & ThisWorkbook.Sheets("Sheet1").Range("J" & ligne) & _
Chr(13) & Chr(13) & "for the period" & " " & ThisWorkbook.Sheets("suivi").Range("C" & ligne) & " " & ThisWorkbook.Sheets("Sheet1").Range("D" & ligne) & _
Chr(13) & Chr(13) & "Thank you!" & _
Chr(13) & Chr(13) & Application.UserName & " " & "-" & " " & "x Tours"
MonMessage.Display
With ThisWorkbook.Sheets("Sheet1").Range("AB" & ligne)
.Value = Date
.NumberFormat = "dd/mm/yyyy"
End With
ActiveWorkbook.Save
Try the code below, explanations inside the code's comments.
Option Explicit
Sub EmailContactsLastRow()
Dim MonOutlook As Object
Dim MonMessage As Object
Dim EmailSht As Worksheet
Dim EmailTo As String
Dim ligne As Long
' set the worksheet object
Set EmailSht = ThisWorkbook.Sheets("Sheet1")
With EmailSht
ligne = .Cells(.Rows.Count, "K").End(xlUp).Row ' get last row with data in column K
EmailTo = .Range("K" & ligne) & ";" & .Range("M" & ligne) & ";" & .Range("O" & ligne) & ";" & _
.Range("Q" & ligne) & ";" & .Range("S" & ligne) & ";" & .Range("U" & ligne) & ";" & _
.Range("W" & ligne) & ";" & .Range("Y" & ligne) & ";" & .Range("AA" & ligne)
End With
Set MonOutlook = CreateObject("Outlook.Application")
Set MonMessage = MonOutlook.CreateItem(0)
With MonMessage
.To = ""
.Cc = ""
.Bcc = EmailTo
.Subject = "Rate request" & " " & "for" & " " & EmailSht.Range("B" & ligne)
.body = "Hello,"
Chr (13) & Chr(13) & "Please send me rate for" & " " & EmailSht.Range("G" & ligne) & " " & "rooms on basis" & " " & EmailSht.Range("H" & ligne) & _
Chr(13) & Chr(13) & "in hotel:" & " " & EmailSht.Range("J" & ligne) & _
Chr(13) & Chr(13) & "for the period" & " " & EmailSht.Range("C" & ligne) & " " & EmailSht.Range("D" & ligne) & _
Chr(13) & Chr(13) & "Thank you!" & _
Chr(13) & Chr(13) & Application.UserName & " " & "-" & " " & "x Tours"
.Display ' <-- this displays the email. not sending it
.send ' <-- this sends the email out
End With
With EmailSht.Range("AB" & ligne)
.Value = Date
.NumberFormat = "dd/mm/yyyy"
End With
ThisWorkbook.Save
End Sub
I am sending mail linked to Excel data. i need to bold and red only few words and i am trying and i am not able to do it. Hope someone from here can help me out. Cells(i,13) has to get bold and Red when i am sending.
toList = Cells(i, 4) 'gets the recipient from col D
eSubject = "Request for KBR transport to " & Cells(i, 2) & " on " & Format(Cells(i, 3), "dd-mmm-yy")
eBody = "Dear " & Cells(i, 1) & vbCrLf & vbCrLf & "Can you please arrange KBR Transport for below PASSENGERS." & vbNewLine & vbNewLine & _
" " & Cells(i, 13) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
"Full Name : " & Cells(i, 6) & vbNewLine & vbNewLine & _
"Nationality : " & Cells(i, 7) & vbNewLine & vbNewLine & _
"Departure/Arrival Date : " & "" & Format(Cells(i, 8), "dd-mmm-yy") & vbNewLine & vbNewLine & _
"Airline : " & Cells(i, 9) & vbNewLine & vbNewLine & _
"Flight Number : " & Cells(i, 10) & vbNewLine & vbNewLine & _
"Departure/Arrival Time : " & Cells(i, 11) & vbNewLine & vbNewLine & _
"Escort required: " & Cells(i, 14) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
" Contact Number: " & Cells(i, 12) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
"Please confirm pick up time and availability." & vbNewLine & vbNewLine ##
Use HTML body instead, then you can use the HTML <b> and <font> tags. It may suit better as you can use a HTML <table> for the data displayed also.
Can someone help me out here with formatting date when I am sending email? In Cell 8 I have date but when I'm sending email date will show as 10/03/2016 but I wanted to show date as 03-Oct-2016. I tried putting code as & Format(Date, "dd/mmm/yy") but when I run the program it will show today's date next to 10/03/2016.
Please see below my coding:
## "Full Name : " & Cells(i, 6) & vbNewLine & vbNewLine & _
"Nationality : " & Cells(i, 7) & vbNewLine & vbNewLine & _
"Departure/Arrival Date : " & Cells(i, 8) & vbNewLine & vbNewLine & _
"Airline : " & Cells(i, 9) & vbNewLine & vbNewLine & _
"Flight Number : " & Cells(i, 10) & vbNewLine & vbNewLine & _
"Departure/Arrival Time : " & Cells(i, 11) & vbNewLine & vbNewLine & _
"Escort required: " & Cells(i, 14) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
" Contact Number: " & Cells(i, 12) & vbNewLine & vbNewLine & _ ##
Change
"Departure/Arrival Date : " & Cells(i, 8) & vbNewLine & vbNewLine & _
to
"Departure/Arrival Date : " & Format(Cells(i, 8), "dd-mmm-yyyy") & vbNewLine & vbNewLine & _