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>"
Related
I'm trying to use Word VBA to send a document to an email recipient. For the most part, it is not difficult. I have this code so far:
With oItem
'Set the recipient for the new email
.To = "person1#mail.com"
'Set the recipient for a copy
.CC = "ccperson#mail.com"
'Set the subject
.Subject = "Blah blah"
End With
My problem is that I have several sender email addresses configured in Outlook, and Outlook is picking the wrong one by default.
Is there a way to specify a sender email address using the method above? Needless to say, the intuitive code line for specifying a sender address (.From = me#wherever.com) does not work. Thank you.
UPDATE:
I finally got my code to work after modifying it using the suggestions from peakpeak and Dimitry below. My changes were
1) to include a reference to the Microsoft Outlook 16 object library so that I could get access to the Outlook.MailItem datatype. The mail would send fine with the code above (without the reference), but would always send with the wrong From address.
2) Declare the mail item as Outlook.MailItem. This seemed to enable the SentOnBehalfOfName field.
3) Used my desired From: email address in the SentOnBehalfOfName field.
Here is the working code:
Dim MAPIMailItem As Outlook.MailItem
Set MAPIMailItem = olkApp.CreateItem(olMailItem) 'Create a new mail message
With MAPIMailItem
.BodyFormat = olFormatPlain
.to = strTo
' SentOnBehalfOfName sets the From field on my machine,
' AFTER I declared MAPIMailItem as Outlook.MailItem
.SentOnBehalfOfName = "fromAddress#foo.com"
.Subject = strSubject
.body = strBody
.attachments.Add strAtt
'.send
.Display
End With
I use this code:
Dim WantedAccount as String ' Set to preferred account name
Set MAPISession = objOutlook.Application.Session 'Get the MAPI Outlook session
Set MAPIMailItem = objOutlook.CreateItem(olMailItem) 'Create a new mail message
With MAPIMailItem
For Each Account In MAPISession.Accounts
If Account = WantedAccount Then
.SendUsingAccount = Account
Exit For
End If
Next
If you are sending through an Exchange account, set the MailItem.SentOnBehalfOfName property (assuming you have the right to send on behalf of the specified mailbox). If you are sending through a POP3/SMTP account, set the MailItem.SendUsingAccount property.
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'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 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")