I have code to populate email from Excel using VBA. I want to retain previous correspondence and also add my signature to the end of the email.
Code that opens the current Outlook item and populates it. The variable ChsBody is assigned earlier.
If OutItem.Class = olMail Then
Set replyEmail = OutItem.ReplyAll
With replyEmail
.Signature
.Body = ChsBody
.Display
End With
Else
MsgBox "ERROR: Make sure email is selected"
End If
Would it be possible to retain the email chain and add my response as a string?
Would it be possible to retain the email chain and add my response as a string?
Change
.Body = ChsBody
to
.HTMLBody = ChsBody & "<br>" & .HTMLBody
In the code you override the Body content with your own:
.Body = ChsBody
Instead, if you want to preserve the existing content, you need to append the string:
.Body = ChsBody & .Body
Note, there are three main ways of dealing with message bodies in Outlook:
Body.
HTMLBody.
The Word editor. The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body.
See Chapter 17: Working with Item Bodies for more information.
Related
I've a macro to send an e-mail through Outlook.
The body is from a cell value with indents. Since the value will change depending on the usage, I need to reference that cell.
Using .HTMLbody
I lose indents which are constructed with CHAR(10).
I keep the default HTML signature.
Using .BODY
Indents are displayed.
The default signature is not constructed as HTML and I lose the images.
My code:
sig = .HTMLBody
body = xlSht.Range("B4").Value
.To = xlSht.Range("B2").Value
.CC = ""
.Subject = xlSht.Range("B1").Value
.body = body & sig
.Display
I have figured it out. I included a helper text in the cell and used
.HTMLBODY = Replace(body,"helper","<br>")
I created a macro in Excel to send emails to various users every time a specific file is updated.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim answer As String
answer = MsgBox("Would you like to save the changes?", vbYesNo, "Save Document")
If answer = vbNo Then Cancel = True
If answer = vbYes Then
'open outlook type stuff
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
'newmsg.Recipients.Add ("Name1")
newmsg.Recipients.Add ("email#xxx.com")
'newmsg.Recipients.Add ("Name2")
newmsg.Recipients.Add ("email#xxx.com")
'add subject
newmsg.Subject = "Notification - Update file"
'add body
newmsg.Body = "This is an automated notification." & vbNewLine & vbNewLine & _
"The XXX file has been recently updated" & vbNewLine & vbNewLine & _
"Please do not reply to this email."
newmsg.Display 'display
newmsg.Send 'send message
'give conformation of sent message
MsgBox "Your document has successfully been saved", , "Confirmation"
End If
'save the document
'Me.Worksheets.Save
End Sub
I would like to add a hyperlink to the body text where it says "The XXX file has been recently updated" so that XXX file is a clickable link to a website.
The Outlook object model supports three main ways of customizing the message body:
The Body property returns or sets a string representing the clear-text body of the Outlook item.
The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create e-mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>Enter the message text here. </BODY></HTML>"
.Display
End With
End Sub
The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.
Note, the MailItem.BodyFormat property allows you to programmatically change the editor that is used for the body of an item.
The last two supports creating a hyperlink in the message body. It is up to you which way is to choose.
If you want to do that, you'll have to write HTML instead of plain text.
This line:
newmsg.Body = "The XXX file has been recently updated"
... would become something like:
newMsg.HTMLBody = "The XXX file has been recently updated".
This is because in Outlook emails with formatting you write HTML text, and a link in HTML is expressed as follows:
your Hyper-text
In Excel I'm using code like this to begin an e-mail message through Outlook:
Set mOutlookApp = GetObject("", "Outlook.application")
Set OutMail = mOutlookApp.CreateItem(0)
With OutMail
.To = "blahblah#blah.com"
.Subject = "More BLAH here"
.HTMLBody = "Message Text" & .HTMLBody ' This preserves the Signature in the message.
.Display
End With
Normally, when I send a (manual) e-mail, I can choose to send it from an address other than my normal one (by pressing the "From" button).
Also, I can choose from one of several signatures I have saved.
How can I accomplish these feats in VBA code?
Change 1 to the account number from which you want to send
.SendUsingAccount = OutApp.Session.Accounts.Item(1)
As far as the signatures are concerned, they are stored in %USERPROFILE%\Application Data\Microsoft\Signatures you can loop through the signatures and choose the relevant one
Set the MailItem.SendUsingAccount property, call Display (at that point Outlook will insert the signature), read the HTMLBody property (it will now contain the signature), merge it with your own data (note that 2 HTML strings cannot be simply concatenated), set the HTMLBody property.
When you click reply all in outlook, it will open a new email box, but show the previous email thread underneath the body of your email. I am trying to Reply All to the selected email in outlook and input information. I want to do this all from a Macro in Excel. My problem is that if I try to write in the body of the Reply All, it erases the entire previous email thread.
Sub test()
Dim mail 'object/mail item iterator
Dim replyall 'object which will represent the reply email
For Each mail In Outlook.Application.ActiveExplorer.Selection
If mail.Class = olMail Then
Set replyall = mail.replyall
With replyall
'.Body = "blah blah hello world" '<-- uncomment and it will delete the thread
.Display
End With
End If
Next
End Sub
Note: the closest I've come is this, but it deletes my signature, the email separator, and the header info from the latest email:
.Body = "blah blah hello world" & mail.body
Any solutions are appreciated! Thanks!
For the plain text, you can do
.Body = "blah blah hello world" & vbCrLf & .Body
If you want to preserve formatting, you will need to insert your string into the replyall.HTMLBody property (you cannot just concatenate two HTML strings).
I am using Excel macros to send an email, where the content is kept in cells. I need to send a non-http link which looks like this
elodms:// followed by an ID
I'm assuming outlook doesn't know this form of link and it doesn't present it as a clickable link when the email is sent, like it does when inserting an email address.
Is it possible to force outlook to view it as a hyperlink? And can it be done through VBA Excel macros?
This is how I have done emails in Excel before.
Email = "Hello, <br><br>" & _
"this is something:" & _
"<br><br> Thank you." & _
"<br><br><br> <a href='elodms://12345'>CLICK HERE</a>"
With OutMail
.To = "you#me.com"
.CC = ""
.BCC = ""
.Subject = "SUBJECT"
.HTMLBody = Email
.send
End With
Is it possible to force outlook to view it as a hyperlink?
You can use the HTMLBody property of the MailItem class. It looks like you just need to insert the hyperlink to the body. To get this working you need to find the tag and insert anywhere before the closing tag the following string:
link text goes here
And can it be done through VBA Excel macros?
Yes, it can be done from Excel. See How to automate Outlook from another program.