I have an email template which has html formatting and place holders to swap out with real values.
In Excel I load the email via the Outlook CreateItemFromTemplate method. If at this point I save the email formatting is preserved.
If I perform a replace on the body most of the formatting is stripped out:
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItemFromTemplate("template.oft") ' <- has lots of html formatting
With OutMail
.Body = Replace(.Body, "#recipient#", "Some other value") ' <- Strips out most formatting!!
.Save ' <- this works fine without the line above.
End With
Thanks to this post : https://stackoverflow.com/a/8473313/569662
My problem was you have to use .HTMLBody rather than .Body :
.HTMLBody = Replace(.HTMLBody, "#recipient#", "Some other value")
Related
I am attempting to capture an email signature with VBA code and insert it automatically into emails.
Based on this answer (Outlook Email and Signature from Excel VBA - .Body vs .HTMLbody), I believe the code below should function as expected - have "Add Body Here" followed by the email signature.
Although I get an error 'Application-defined or object defined error' on the line .HTMLBody = "<p>Add Body Here.</p>" & .HTMLBody'.
Dim OApp As Object
Dim OMail As Object
Dim Signature As Variant
Set OApp = CreateObject("Outlook.Application")
Set OMail = OApp.CreateItem(0)
With OMail
.Display
.Subject = "Subject"
.HTMLBody = "<p>Add Body Here.</p>" & .HTMLBody
.Display
End With
First of all, call the Display method before making any modifications to the message body to grab the signature as is:
With OMail
.Display
.Subject = "Subject"
.HTMLBody = "<p>Add Body Here.</p>"
End With
Second, there is no need to call the Display method twice in the code.
Third, keep in mind that HTMLBody property is an HTML string which represents the message body. While the OOM handles badly formatted HTML strings, the best practice to deal with a well-formatted HTML strings, so that means you need to insert the additional content right after the opening <body> tag if you need to get it in the beginning or just assign it from scratch:
.HTMLBody = "<p>Add Body Here.</p>"
I received a request from one of my users to create a macro in Outlook.
His requirements were to have an email header that is bold, highlighted and in italics.
This part we were able to accomplish, but he has also requested that the cursor end up in the body of the email just below the header ready to input text using his regular formatting options (font, size, color).
Currently when our macro runs, the cursor ends up at the beginning of the email header.
If you move the cursor to the line below the header then the text is still bold and italicized.
Is there a way to reset the text formatting after the header is inserted, and is there a way to specify that the cursor end up below the header? This is what we have come up with so far:
Sub Testmacro2()
Dim olApp As Outlook.Application, olEmail As Outlook.MailItem, signature As String
Set olApp = CreateObject("Outlook.Application")
Set olEmail = olApp.CreateItem(0)
With olEmail
.Display
End With
signature = olEmail.HTMLBody
With olEmail
.HTMLBody = "<HTML><BODY><span style='background:yellow;mso-highlight:yellow'><em><b><p style=font-size:14pt>Privileged & Confidential Attorney Client Communication & Work Product.</b></em><br></span></BODY></HTML>" & vbNewLine & signature
End With
Set olEmail = Nothing
Set olApp = Nothing
End Sub
You will need to work with Inspector.WordEditor (returns an instance of the Word.Document object), namely Inspector.WordEditor.Application.Selection.
Inspector object can be retrieved from MailItem.GetInspector.
I am trying to create a simple script to create a html message, and I would like to keep as much as possible of the default values.
In my case when I create a new message using Home -> New mail it would always create a mail with default font [Calibri 11'], format text Html, and also with a signature.
If I use the function Application.CreateItem(olMailItem) it creates the mail without issues.
However as soon as I declare an html body with .HTMLBody it will overwrite the format: font [Times New Roman 12'], and also without a signature.
Even more surprising: if I use the 'trick' found online to keep the signature (see code bellow) then the font would be then overwritten to [Calibri 10'] (one point bellow the default)
Any recommendation/explanation?
Simple script to create an html mail (change of font + no signature) :
Sub TestMessage1()
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.To = "test#domain"
.Subject = "test Mail"
.HTMLBody = "<p>Hello,<br><br>Could you please review this program<br><br>Regards,</p>"
.Display
End With
End Sub
write html mail with a signature (different change of font):
Sub TestMessage2()
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.To = "test#domain"
.Subject = "test Mail"
.Display
.HTMLBody = "<p>Hello,<br><br>Could you please review this program<br><br>Regards,</p>" & objMsg.HTMLBody
End With
End Sub
You need to paste the text or well-formatted HTML markup right after the <body> tag and before the closing </body> tag. In that case all format preferences will be preserved.
I'm trying to send an Outlook email from VBA Excel. I've got everything declared correctly, as far as I can tell. I'm having issues with changing the sender and the font size.
The from is a secondary email, also on Outlook, which I have access to.
The font issue is that I can't seem to achieve font size 11 when using the below code.
Sender:
With OutMail
.Display
.Sender = "someone#example.com"
'.SentOnBehalfOfName = "someoneelse#example.com"
.To = origintext
.Subject = "Location Verification"
.BodyFormat = 2 'olFormatHTML
.HTMLBody = fMsg & fMsg2 & fMsg3 & signature
'.Body = signature
.Display
End With
Where fMsg, fMsg2, and fMsg3 are strings. The signature is declared earlier in the code with:
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
OutMail.Display
signature = OutMail.HTMLBody
I do this to get the signature as it seems to be impossible to use something such as OutMail.Signature = *Signature1*.
I understand there is an OutMail.SentOnBehalfOf = hello#world.com but that does not seem to work with OutMail.BodyFormat = 2 which sets the body to HTML format.
Font:
An example of my HTML body is as follows:
fMsg = "<p><font face = ""Calibri(Body)"" font size=""3"" color=""black"">Hello,</font></p>"
However, the issue that comes with this is font size=""3"" does not actually give font size 3, it gives font size 10. I'm trying to get to 11. font size=""4"" produces size 13.5.
TL;DR:
What's the VBA code to send an Outlook email from my second email account?
What's the VBA code to get font size 11 using HTML format?
SentOnBehalfOfName is a little tricky. See here where it works when it precedes Display. SentOnBehalfOf not working in Excel 2010 VBA Code
You can use "style=font-size:11pt" as described here Change HTML email body font type and size in VBA
The SendUsingAccount property of the MailItem class allows to set an Account object that represents the account under which the MailItem is to be sent. For example:
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("someone#example.com")
oMail.Recipients.ResolveAll
oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
Try to use the Word object model to change the font size in emails. See Chapter 17: Working with Item Bodies for more information.
I have code which alters the sensitivity of a message. It does work but the font size changes. Is something wrong with the code?
Public Sub MakeThisConfidential()
Application.ActiveInspector.CurrentItem.Sensitivity = olConfidential
Application.ActiveInspector.CurrentItem.Save
Set MsgSub = Outlook.Application.ActiveInspector.CurrentItem
Set objMail = Outlook.Application.ActiveInspector.CurrentItem
Subject = MsgSub.Subject
MsgSub.Subject = Subject & " - [CONFIDENTIAL]"
email = objMail.Body
info = vbNewLine & "AUTO TEXT: This message has been marked as 'CONFIDENTIAL' please treat it as such"
objMail.Body = email & info
End Sub
You probably want to use the .HTMLBody property instead of .Body.
Also, the concatenation character in VBA is &, not +.
If using .Body you will need to use vbNewLine to create a line break. If using .HTMLBody you will need to use HTML/CSS syntax such as <p></p> to create space.