Empty HTML Body while creating Outlook meeting invite using VBA - html

I'm trying to create an Outlook meeting invite.
The meeting invite is created. Subject and other fields are also updated.
I get an empty body when I use .HTMLBody.
Code I tried:
Set OutApp = Outlook.Application
Set OutMeet = OutApp.CreateItem(olAppointmentItem)
With OutMeet
.Subject = Subj
.RequiredAttendees = ActiveSheet.Range("G9").Value
ThisWorkbook.Sheets("Sheet1").Select
.Start = ThisWorkbook.Worksheets("Sheet1").Range("D2").Value
.Duration = 10
.Importance = olImportanceHigh
.ReminderMinutesBeforeStart = 15
ThisWorkbook.Sheets("Sheet2").Select
.BodyFormat = olFormatHTML
.HTMLBody = "<html><head></head><body>Hi</body></html>"
Application.Visible = True
MeetingStatus = olMeeting
.Location = "Microsoft Teams"
.Display
End With
Set OutMeet = Nothing
Set OutApp = Nothing
End Sub

There is no HTMLBody for appointment items.
See https://social.msdn.microsoft.com/Forums/en-US/1e7cc383-bd2f-4d18-8cd1-9489bb3011ea/getset-appointment-body-type-fromto-rtfhtml-plain-text?forum=outlookdev

Make sure you have your outlook reference libraries turned on and then try
.Body = "Hi"
This worked for me

Related

How to include "mailto" in body of email?

I send an email through Outlook. I am trying to put an email address link within the body of the email, so that one can click on it and an email opens.
E.g. "If you have any questions, please contact us at ABC", where the "ABC" is the link that represents email address, ABC#gmail.com (not the real address).
I understand, from scouring the web, this is the syntax:
<a href="mailto:ABC#gmail.com\>ABC\</a>
I borrowed code that uses HTML to format a message in the body of an email, and replaced a line with what I wanted.
I received a Compile Error (which I think is actually a syntax error in this case?).
When debugging, the "mailto" text was highlighted.
My insertion:
ABC mailbox
.HTMLBody = "\<HTML\>\<BODY\>\<a href="mailto:ABC#gmail.com"\>ABC mailbox\</a>\</BODY\>\</HTML\>"
Sub SendEmailformattext()
'Update by Extendoffice.com
Dim xRg As Range
Dim xRgEach As Range
Dim xRgVal As String
Dim xAddress As String
Dim OutlookApp As Object
Dim MItem As Object
On Error Resume Next
xAddress = ActiveWindow.RangeSelection.Address
Set xRg = Application.InputBox("Please select email address range", "KuTools For Excel", xAddress, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set OutlookApp = CreateObject("Outlook.Application")
Set MItem = OutlookApp.CreateItem(olMailItem)
Set xRg = xRg.SpecialCells(xlCellTypeConstants, xlTextValues)
For Each xRgEach In xRg
xRgVal = xRgEach.Value
If xRgVal Like "?*#?*.?\*" Then
Set MItem = xOutApp.CreateItem(olMailItem)
With MItem
.Display
.To = xRgVal
.Subject = "Test"
.HTMLBody = "\<HTML\>\<BODY\>\<a href="mailto:ABC#gmail.com"\>ABC mailbox\</a>\</BODY\>\</HTML\>"
'.Send
End With
End If
Next
Set xMailOut = Nothing
UNCLASSIFIED
Set xOutApp = Nothing
Application.ScreenUpdating = True
End Sub
The original code worked so I know the mailto text is the problem.
Use the Chr(34) function if you need to use double quotes in the string:
.HTMLBody = "\<HTML\>\<BODY\>\**<a href="& Chr(34) & "mailto:ABC#gmail.com" & Chr(34) & "\>ABC mailbox\</a>**\</BODY\>\</HTML\>"

Move the table right to align with my texts in Outlook body

I am trying to sent an email from outlook using Excel Macro and I need a solution on how I can move the tables I copy paste from the Excel to Outlook body, which will always get aligned to left side, and I want it to be moved to little right so that I will get a perfectly fit/alignment with the other contents I have on the top.
My Code
Sub Table_CopyPaste()
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Set outlook = CreateObject("Outlook.Application") Set newEmail = outlook.CreateItem(0)
StrBody1 = "<o:p> </o:p><p class=MsoNormal><span lang=EN-US style='color:#1F497D'><span style='font:11.0pt 'Calibri'>Hello,</Span></p><p class=MsoNormal><span style='font:11.0pt 'Calibri(Body)'>Attached you can find a file with all your Status for month 10_2019. </p></span><p class=MsoNormal><span style='font:11.0pt 'Calibri(Body)'>Below you can find an overview of your current status and your unit status.</span></span></p>" _
& "<p class=MsoListParagraph style='margin-left:53.4pt;text-indent:-18.0pt;mso-list:l0 level1 lfo2'><span style='font:11.0pt 'Calibri'><span lang=EN-US style='color:#1F497D'>1) We have extended the report with additional information, so you can develop a more complete view on your status:" _
& "<br>" _
& "<br> - &nbsp HR" _
& "<br> - &nbsp Accounts" _
& "<br> - &nbsp Finance" _
With newEmail
.To = "Test#mail.com"
.CC = ""
.BCC = ""
.Subject = "Data"
.HTMLBody = StrBody1
.Display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Sheets("Statistics_Sheet").Range("A3:D6").Copy
pageEditor.Application.Selection.Start = Len(.HTMLBody)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
.Display
'.Send
Set pageEditor = Nothing
Set xInspect = Nothing End With
Set newEmail = Nothing Set outlook = Nothing
End Sub
This is the first time I am working on integrating outlook with macro, so no much idea how to solve this. The code is working fine once we run this code I need the table to be placed aligned with the bullet point 'Finance'
After too many search here and there, I find that its not possible to move it but a workaround here is to add some extra blank cells in that range so that when it copy pasted to outlook you will get it aligned to the text (Adjust the width of the cells to change the alignment)
But I find a code which work like a charm,
Sub PublishTable()
Dim WB As ThisWorkbook, P As String, WS As Worksheet, Rng As Range, New_WB As Workbook, RNG2 As Range, FolderPath As String
Set WB = ThisWorkbook
Set WS = WB.Sheets("Statistics_Sheet")
FolderPath = Application.ActiveWorkbook.Path
Set Rng = Sheets("Statistics_Sheet").Range("C3:F6")
P = FolderPath & "\Calculation_of_exception_status.html"
Workbooks.Add
Set New_WB = ActiveWorkbook
ThisWorkbook.Activate
Rng.Copy
New_WB.Activate
ActiveCell.PasteSpecial xlPasteValues
ActiveCell.PasteSpecial xlPasteFormats
ActiveCell.PasteSpecial xlPasteColumnWidths
ActiveCell.PasteSpecial xlPasteFormats
New_WB.PublishObjects.Add(xlSourceRange, P, New_WB.Sheets(1).Name, New_WB.Sheets(1).UsedRange.Address, xlHtmlStatic).Publish (True)
ActiveWorkbook.Close SaveChanges:=False
Dim fso As New FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim Final_File As Scripting.TextStream
Set Final_File = fso.OpenTextFile(P, ForReading)
StrTable2 = Final_File.ReadAll
End Sub
And when you use Strtable2 in your outlook body use below code, adjust '20.3pt' according to your requirement.
olMailItm.HTMLBody = "<table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 style='margin-left:20.3pt;border-collapse:collapse'>" & StrTable2 & "</Table>"
If it's an actual HTML <table> element that it inserts, you can add a stylesheet to the .HTMLBody
At the top, Dim a variable:
Dim sStyles As String
sStyles = "<style> table {margin-left:150px;} table table {margin-left:0px;} </style><br>"
And then where you set the .HTMLBody, add it in like this:
.HTMLBody = sStyles & StrBody1
Then adjust the 150 number in the code to whatever you want to get it to align where you want it.

VBA Excel Email Formatting Issue

My data is set up like this:
-On Excel, I have a worksheet, "Test1", which has a list of people I want to send emails to. Like such;
No. Version Company Name Email
1 R x Max max#abc.com
2 E y Bill bill#abc.com
3 C z Scott scott#abc.com
The emails I want to send differ slightly by type. My current code is as follows:
Sub mailTest()
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
For i = 2 To 3
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
mailBody = Worksheets("BODY").Cells(1, 1).Value & Worksheets("Test1").Cells(i, 4) _
& Worksheets("BODY").Cells(2, 1).Value
With olMail
.To = Worksheets("Test1").Cells(i, 5).Value
.Subject = Worksheets("Test1").Cells(i, 3).Value
.HTMLBody = mailBody
.Display
End With
Set olMail = Nothing
Set olApp = Nothing
Next
End Sub
The code above displays the emails as the main body is in Cell A2 of sheet "BODY". The "Hello" is stored in Cell A1, and also displays fine as I have HTML coding in both these cells to set the font to Calibri and the size to 3.
The issue is when I'm pulling the names of people from sheet Test1 in Column 4 using the code;
& Worksheets("Test1").Cells(i, 4)
It is displaying with a different font, defaulting to Times New Roman. What I'm left with is everything displaying in Calibri, with just their names being pulled in Times New Roman.
Is there any way to fix this without adding HTML code in each of the name cells? The actual file I'm working on has about 500 names so manually doing it would be a pain...
Thanks everyone.
Try replace
.HTMLBody = mailBody
with
.HTMLBody = "<!DOCTYPE html><html><head><style>"
.HTMLBody = .HTMLBody & "body{font-family: Calibri, ""Times New Roman"", sans-serif}"
.HTMLBody = .HTMLBody & "</style></head><body>"
.HTMLBody = .HTMLBody & mailBody & "</body></html>

Global HTML-Body of an E-Mail created in a VBA code

I have the following VBA code for an E-Mail:
Sub Global_Email_Message()
Content = "<style> p {background-color: #d9d9d9} </style><p> This message should be global so I can use it in every sub for an E-Mail </p>"
If ExitAll = False Then
Set OApp = CreateObject("Outlook.Application")
Set OMail = OApp.CreateItem(0)
With OMail
.display
End With
signature = OMail.HTMLBody
With OMail
.To = "test#test.de"
.Subject = "test"
.HTMLBody = Content
End With
Set OMail = Nothing
Set OApp = Nothing
Else
End If
End Sub
This code works perfectly.
Now, I want to achieve that the message and the style in the variable "Content" is global so I can use it in different E-Mail-Subs. How can I globalize the "Content" variable for different E-Mail-Subs?
You need to declare the content variable with public outside the function:
Public content as String
Sub Global_Email_Message()
...

Interop.Outlook Message in HTML displayed as Plain Text

I recently switched from using EWS to using Interop.Outlook (see this article). The process is extremely easy to use!
Unfortunately, I have one problem that did not exist in EWS: Outlook does not process the HTML body even when BodyFormat is set to true. In this code sample (VB.NET), the MessageBody does start with < HTML. With debug, I verified that BodyFormat was set to HTML when display was executed. Nevertheless, the email body is displayed as plain text.
Dim Outlook As New Outlook.Application
Dim mail As Outlook.MailItem = DirectCast(Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem), Outlook.MailItem)
With mail
.To = Addr
.Subject = Subject
.Body = MessageBody
.BodyFormat = If(MessageBody.ToLower.StartsWith("<html"),
Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML,
Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatPlain)
.Display(Modal)
The exact same body text, when using EWS displays correctly.
.Body = MessageBody
The Body property of the MailItem class is a string representing the clear-text body of the Outlook item (without formatting). You need to set the body format first (if required). By default Outlook uses the HTML format.
With mail
.To = Addr
.Subject = Subject
If(MessageBody.ToLower.StartsWith("<html")) Then
.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML
.HTMLBody = MessageBody
Else
.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatPlain
.Body = MessageBody
End If
.Display(Modal)
Use the HTMLBody property for setting an HTML markup.
Or just simply:
With mail
.To = Addr
.Subject = Subject
If(MessageBody.ToLower.StartsWith("<html")) Then
.HTMLBody = MessageBody
Else
.Body = MessageBody
End If
.Display(Modal)