Add a html body to this code

anilkumard

New Member
Joined
Nov 18, 2016
Messages
2
what this code do?
this code sends out emails

why this code? why not other code?
well other codes interrupt my other activities or i would say i have to sit idle watching the screen while they take up the screen. the below given code silently does the job without my attention.

what am i trying to do ?
i have a daily task of sending out emails with reports to my people, this code sends only text as the body of the email. As we all know most of the reports consists of rows and columns with numbers in it, so is my report too.

i am unable to put the html code or a part of word document or a part of excel in the body of every email i send out.

i heartily thank you and appreciate the time you have taken to consider this as your challenge.
i hope i will get a solution from this forum members.

Thank you and here is the code i picked up from a website -
Rich (BB code):
Sub OutlookMail_3()
'Automate Sending Emails from Excel, using Outlook. Send multiple mails to ids sourced from the Host Workbook's sheet.
'Automating  Outlook from Excel, using Late Binding. You need not add a reference to  the Outlook object library in Excel (your host application), in this  case you will not be able to use the Outlook's predefined constants and  will need to replace them by their numerical values in your code.
 
'variables declared as Object Type, which can be a reference to any object: Dim oApplOL As Object Dim oMiOL As Object Dim lastRow As Long Dim ws As Worksheet Dim strMailSubject As String Dim strMailMessage As String 'set worksheet: Set ws = ActiveWorkbook.Sheets("Sheet1") 'Email ids are entered in column A of Sheet5 - determine last data row in column A of the worksheet: lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row 'Create a new instance of the Outlook application, if an existing Outlook object is not available. 'Set the Application object as follows: On Error Resume Next Set oApplOL = GetObject(, "Outlook.Application") 'if an instance of an existing Outlook object is not available, an error will occur (Err.Number = 0 means no error): If Err.Number <> 0 Then​
Set oApplOL = CreateObject("Outlook.Application")​
End If 'disable error handling: On Error GoTo 0 'ignores an invalid mail id syntax, and code execution will resume On Error Resume Next​
'loop through all cells in column A:​
For i = 1 To lastRow​
'validate mail syntax: 'Like operator in vba: If the string satisfies the specified pattern, it will return True. * denotes zero or more characters, ? denotes a single character. 'The following statement returns True if the string has an "@" and atleast one character before it, and atleast one character after it, then followed by "." and atleast one character after it.​
If Trim(ws.Cells(i, 1).Value) Like "*?@?*.?*" Then​
'pick mail subject from Range("C1"):​
strMailSubject = ws.Cells(1, 3)​
'Add text to mail message and also pick text from Range("C2") and Range("C3"):​
strMailMessage = "Hello " & ws.Cells(i, 2) & vbCrLf & vbCrLf & ws.Cells(3, 3) & vbCrLf & vbCrLf & "Best Regards," & Chr(13) & "Administrator"​
'create mail item:​
'Built-in constant olMailItem has been replaced by its value 0.​
Set oMiOL = oApplOL.CreateItem(0)​
With oMiOL​
'pick mails ids from column A: .To = ws.Cells(i, 1) 'Built-in constant olImportanceLow has been replaced by its value 0. .Importance = 0 .Subject = strMailSubject .Body = strMailMessage .ReadReceiptRequested = False​
.send​
End With​
End If​
'set a 2 seconds time interval: Application.Wait (Now + TimeValue("0:00:02"))​
Next i​
'clear the object variables: Set oApplOL = Nothing Set oMiOL = Nothing​
End Sub​
 
Last edited by a moderator:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi - just a quick thought. Would the following work:

Replace this:
Code:
[COLOR=#000000].Body = strMailMessage


with this:
[/COLOR]
Code:
<code>.BodyFormat = 2
.[B]HTMLBody = "<HTML>******>" & [COLOR=#000000]strMailMessage [/COLOR]& "</BODY></HTML>"[/B]</code>

I think the 2 is equivalent to olFormatHTML. But as you are late binding you will have to use a number value, rather than the Outlook VBA name.

Also, I'm not sure if the HTML tags are actually required - you may need to experiment.

Let me know if it works.
 
Upvote 0
i am trying to figure out where and how to paste the html code, here is the word document & html code of same.


Hi Kumar,


Here is the report of today’s call log
Executive
No of calls
Total Call log
Kumar2551:55:00
Kumar3380:53:06
Kumar41092:23:04
Kumar5691:15:24

<tbody>
</tbody>

Regards
Kumar
HTML:
      <p style="margin-bottom: 0in; line-height: 100%;"><span style="font-family: Calibri,sans-serif;">Hi Kumar,</span></p>
<p style="margin-bottom: 0in; line-height: 100%;"> </p>
<p style="margin-bottom: 0in; line-height: 100%;"><span style="color: #000080;"><span style="font-family: Calibri,sans-serif;"><strong>Here is the report of today’s call log</strong></span></span></p>
<p style="margin-bottom: 0in; line-height: 100%;"> </p>
<table width="100%" cellspacing="0" cellpadding="4">
<tbody>
<tr valign="top">
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding: 0.04in 0in 0.04in 0.04in;" width="33%">
<p align="center"><span style="font-family: Calibri,sans-serif;"><strong>Executive</strong></span></p>
</td>
<td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding: 0.04in 0in 0.04in 0.04in;" width="33%">
<p align="center"><span style="font-family: Calibri,sans-serif;"><strong>No of calls</strong></span></p>
</td>
<td style="border: 1px solid #000000; padding: 0.04in;" width="33%">
<p align="center"><span style="font-family: Calibri,sans-serif;"><strong>Total Call log</strong></span></p>
</td>
</tr>
<tr valign="top">
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding: 0in 0in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">Kumar2</span></p>
</td>
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding: 0in 0in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">55</span></p>
</td>
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding: 0in 0.04in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">1:55:00</span></p>
</td>
</tr>
<tr valign="top">
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding: 0in 0in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">Kumar3</span></p>
</td>
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding: 0in 0in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">38</span></p>
</td>
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding: 0in 0.04in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">0:53:06</span></p>
</td>
</tr>
<tr valign="top">
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding: 0in 0in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">Kumar4</span></p>
</td>
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding: 0in 0in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">109</span></p>
</td>
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding: 0in 0.04in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">2:23:04</span></p>
</td>
</tr>
<tr valign="top">
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding: 0in 0in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">Kumar5</span></p>
</td>
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding: 0in 0in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">69</span></p>
</td>
<td style="border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding: 0in 0.04in 0.04in 0.04in;" width="33%">
<p><span style="font-family: Calibri,sans-serif;">1:15:24</span></p>
</td>
</tr>
</tbody>
</table>
<p style="margin-bottom: 0in; line-height: 100%;"> </p>
<p style="margin-bottom: 0in; line-height: 100%;"><span style="font-family: Calibri,sans-serif;">Regards</span></p>
<p style="margin-bottom: 0in; line-height: 100%;"><span style="font-family: Calibri,sans-serif;">Kumar</span></p>
can you please guide me how to do so, or is there a better way to make it work?
 
Last edited by a moderator:
Upvote 0
Kumar

Instead of Body you need to use HTMLBody, as Mark80 suggested.

Try doing that and try putting the HTML you tried to post here in the variable strMailMessage.
 
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,930
Members
449,195
Latest member
Stevenciu

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