Reply All in Outlook from Excel Macro - excel

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).

Related

How to send multiple hyperlinks with mail in Excel VBA, depending on the cell value

There is and light order managment system where you can order a few items. When you have chosen your order, you click a button, and the rows that is order and number of items is copied to a new confirmation sheet. This sheet is then supposed to be sent to a chosen reciever by oMail. This works fine.
But in one of the columns there is a certificate on PDF(hyperlink), that is linked to a server(local file on my computer:))
I'm trying to send this PDF's as multiple hyperlinks in the mail, but that is no sucess :p. The thing I want to do is check if the cell is empty, if not, attach the hyperlink(s) in my stringbody. And send the mail.
Here is my code:
I use a function also to make the range to html format:
It's more useful to edit e-mail body via GetInstector
For example:
Dim myInspector As Object'inspector object
Dim wdDoc As Object''document
Dim myitem As Object'mailitem
Set myitem = CreateObject("Outlook.Application").CreateItem(0)'creating mailitem from Excel
Set myInspector = myitem.GetInspector'set inspector to edit mailitem
Set wdDoc = myInspector.WordEditor'set MS Word to edit mailbody
With myitem'here you are setting the e-mail properties
.To = Email'fill to e-mail
.Subject = Subja'Subject
.Display'display mailitem etc.
End With
'here edit the body with MS Word methods
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.item(1).Range.Text = "Something you want in 1st paragraph" & vbCr
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.item(2).Range.Text = "Something else you want in 2nd paragraph " & vbCr
So, actually you edit e-mail programmatically the same as you do it in Word. It makes useless long and complicated string with HTML-tags. You add MS Word objects and forms.

How can I reply to email without losing the previous email chain?

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.

Merge "Rows data" and "Range/selection" data as Mail body in VBA using Outlook

I am new to VBA, Ron de Bruin content have been a great help but I'm stuck with this problem. From Ron de Bruin's topic: How can I merge "Mail a row or rows to each person in a range" & " Mail Range/Selection in the body of the mail" so I can send it as HTML mail body using outlook?
I've looked everywhere but cannot find an answer to it, and my VBA knowledge limits the finding.
I used the same code from Ron de Bruin:
Mail Range/Selection in the body of the mail: https://www.rondebruin.nl/win/s1/outlook/bmail8.htm
Mail Range/Selection in the body of the mail: https://www.rondebruin.nl/win/s1/outlook/bmail2.htm
What I did a bit was:
.HTMLBody = RangetoHTML(rng) & "<br>" & _
"Find below the Absolute goal and its weightage:" & "<br>" & _
Sheets("Sheet6").Range("M1:P12").Value & "<br><br><br>" & _
"Regards," & "<br>" & _
"XXXXX" & "<br>"
The email body comes blank.
I am looking for something like:
hello XX,
Please find below the performance details:
Login ID Agent TM Handles Count
XXXX asdsd#gmail.com yyy 1234 4567
Find below the absolute goals:
Skill metric handles count
ggggg hhhhh 23465 77777
Regards,
ddddd
Make sure that you set a well-formed HTML markup. Otherwise, Outlook will not be able to display the message body.
The HTMLBody property of the MailItem class returns a string representing the HTML body of the specified item.
Sub CreateHTMLMail()
'Creates a new email item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create email 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
If you want to preserve the existing body markup you need to find the closing </body> tag
and insert your piece of HTML markup before the closing tag. And make sure that you end up with a well-formed HTML markup after making any changes.
Finally, you may find the Working with Outlook HTMLBody – a guide for Office developers article helpful.

How to insert hyperlink into email body

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

How do I send a non-http hyperlink in an email message generated by Excel macros?

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.

Resources