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.
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>"
We currently have an email automatically created by Excel using VBA, with subject, recipient, message body with template text all filled in.
Sub CreateMail(Optional sFile As String = "")
'Create email to send to requestor with attachment sFile
'Declarations
Dim app As Outlook.Application
Dim msg As Outlook.MailItem
Dim send_to As Recipient
Dim send_tos As Recipients
'Initiations
Set app = CreateObject("Outlook.Application")
Set msg = app.CreateItem(olMailItem)
Set send_tos = msg.Recipients
Set send_to = send_tos.Add("receiver#email.com")
send_to.Type = 1
'Create message
With msg
.SentOnBehalfOfName = "sender#email.com"
.Subject = "This is the email subject"
.HTMLBody = "This is the email body" & vbCrLf
'Resolve each Recipient's name.
For Each send_to In msg.Recipients
send_to.Resolve
Next
If Len(sFile) > 0 Then
.Attachments.Add sFile
End If
.Display
End With
End sub
After making some manual changes to the email that is created, we'd like to send it and have a copy saved to a folder on the file system automatically (in addition to the usual sent folder in Outlook). Is there a way to do this all within Excel VBA?
I suspect it might be possible using Outlook VBA, however the folders are defined in Excel and we'd like to keep the code together in the one file.
What is your code for sending email? This works for me in an Excel VBA module:
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = "email address"
.Subject = "Test"
.HTMLBody = "Test " & Now
.DeleteAfterSubmit = True 'to not retain in sent folder
.Display
.SaveAs "C:\filepath\Test.txt", 0
' .Send
End With
However, guess the real trick is allowing edit of the email before saving file. So far not seeing solution for that. Unfortunately the code execution does not pause while the message window is open. I was hoping for the pause since Office is supposed to be an integrated suite of apps - like opening a form in Access in dialog mode which does pause execution of code.
With code in Excel only, monitor the SentItems folder.
Utilizing Outlook Events From Excel
Confirm the mail from a unique ID.
The unique ID could be in the subject or body.
You could try saving the unique ID in PR_SEARCH_KEY. It is the same idea How, can get the exact sent Email from Sent Items folder? and How to uniquely identify an Outlook email as MailItem.EntryID changes when email is moved
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'm trying to write a macro that sends an automatic notification to specific addresses before sending the original email. (Like a cc, without actually using cc.)
The content of the original formatted email, (including text, tables, and pictures,) should be copied and pasted into a new email which is then automatically sent. Everything works when I just display the message, but not when actually sending the email.
Here is my code:
Dim objMsg As Outlook.MailItem
Dim activeMailMessage As Outlook.MailItem
Dim BodyText As Object
' Create the message.
Set objMsg = Application.CreateItem(olMailItem)
'copy body of current item
Set activeMailMessage = ActiveInspector.CurrentItem
activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy
'paste body into new email
Set BodyText = objMsg.GetInspector.WordEditor.Range
BodyText.Paste
'set up and send notification email
With objMsg
.To = "test#domain.com"
.Subject = "text"
.Send
End With
The text should be pasted into the body like this, but it won't paste:
With objMsg
.To = "test#domain.com"
.Subject = "test"
.body = bodytext.paste
.Send
End With
When I use .display the correct content is displayed. But when I send it directly (without first using .display), all of all information is lost and an empty email is sent. What can I do?
I could add a bcc in the original email to achieve the same result, but the original email does not always send, whereas this notification should be.
Your code is never actually setting the Body of the e-mail in the objMsg object. It is working when you have objMsg displayed because your interacting with the 'Inspector'.
If you directly set either the HTMLBody (if you want to retain formatting), or the Body property on objMsg then it will work as in the below example.
With objMsg
.HTMLBody = activeMailMessage.HTMLBody
.To = "test#domain.com"
.Subject = "text"
.Send
End With
Bob, regarding your question on images that are embedded within the e-mail being lost with the above approach. An alternate solution could be to use the MailItem's Copy method to create your new MailItem exactly as the original Item. This will also retain who the e-mail is being sent to you need to clear this to make sure only the intended recipients receive it.
Dim objMsg As Outlook.MailItem
Dim activeMailMessage As Outlook.MailItem
' Create the new message.
Set objMsg = Application.CreateItem(olMailItem)
' Assign the current item to activeMailMessage
Set activeMailMessage = ActiveInspector.CurrentItem
' Copy the current item to create a new message
Set objMsg = activeMailMessage.Copy
' Clear any existing recipients of the e-mail, as these will be retained in the Copy
While objMsg.Recipients.Count > 0
objMsg.Recipients.Remove 1
Wend
'set up and send notification email
With objMsg
.To = "test#domain.com"
.Subject = "text"
.Send
End With
This should retain your images and other attachments as they were in the original e-mail.
Try to call the Save method after calling the Paste method.
I have a Word document (saved as a macro-enabled template) that allows a user to fill out a "time off" form using drop-down boxes. It's set up to email the supervisor an attachment with the request; however, the document saves itself when used, which could potentially reveal sensitive information to the next person viewing the form.
Would it be possible to send the document content as plain text in the body of the email (we are using Outlook) so that the document doesn't have to be saved? Or is there a function to add that will allow for the email to send and then the contents to be reset and then re-saved?
Here is my current code:
Private Sub CommandButton21_Click()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument
Doc.Save
With EmailItem
.Subject = "TIME OFF REQUEST"
.Body = "THE ATTACHED DOCUMENT IS A DAY OFF REQUEST"
.To = "Email"
.Importance = olImportanceNormal
.Attachments.Add Doc.FullName
.Send
End
With Application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub
The Content property of the Document class returns a Range object that represents the main document story. The Text property returns the plain, unformatted text of the range. So, here is the code you are looking for:
.Body = ActiveDocument.Content.Text
Be aware, Outlook uses Word as an email editor. You can use the Word object model for manipulating the message body. You can read more about that in the Chapter 17: Working with Item Bodies .
Finally, you may find the How to automate Outlook from another program article helpful.