Log in

View Full Version : Outlook VBA macro to get MailItem's current(most recent) message body



merlinsdemon
05-18-2015, 05:25 AM
Hi all,

I'm making a macro to copy some information from an outlook mail item, to the clipboard. Everything is working exactly as I want, except one thing. When I obtain the Message body from the Mail Item, if it's a ongoing chain, it copies the entire chain. I just want to copy the most recent received information(message body). Here is my Macro.

Sub Copy_Forward()
Dim oMail As MailItem
Dim DataObj As MSForms.DataObject
Dim str1 As String
Dim str2 As String
Dim str3a As String
Dim str3b As String
Dim str4 As String
Dim str5 As String

Set oMail = ActiveExplorer().Selection.Item(1)
Set DataObj = New MSForms.DataObject
str1 = "From: " + oMail.SenderName + Chr(13)
str2 = "Sent: " + Format(oMail.SentOn, "dddd MMMM d,yyyy h:mm AMPM") + Chr(13)
str3a = "To: "
For Each Recipient In oMail.Recipients
str3b = str3b + Recipient.Address + "; "
Next Recipient

str4 = Chr(13) + "Subject: " + oMail.Subject
str5 = Chr(13) + oMail.Body
DataObj.SetText str1 & str2 & str3a & str3b & str4 & str5
DataObj.PutInClipboard
End Sub

merlinsdemon
05-18-2015, 06:01 AM
Solved:
This is what I ended up doing, splitting the Body with the delimiter "From: ", then taking the first item in the resulting array.

Sub Copy_Forward()
Dim oMail As MailItem
Dim DataObj As MSForms.DataObject
Dim str1 As String
Dim str2 As String
Dim str3a As String
Dim str3b As String
Dim str4 As String
Dim str5 As String
Dim v As Variant

Set oMail = ActiveExplorer().Selection.Item(1)
Set DataObj = New MSForms.DataObject
str1 = "From: " + oMail.SenderName + Chr(13)
str2 = "Sent: " + Format(oMail.SentOn, "dddd MMMM d,yyyy h:mm AMPM") + Chr(13)
str3a = "To: "
For Each Recipient In oMail.Recipients
str3b = str3b + Recipient.Address + "; "
Next Recipient


str4 = Chr(13) + "Subject: " + oMail.Subject
str5 = Chr(13) + oMail.Body
v = Split(str5, "From: ")
str5 = v(0)

DataObj.SetText str1 & str2 & str3a & str3b & str4 & str5
DataObj.PutInClipboard
End Sub

OutdorBanker
05-19-2015, 01:36 PM
Here is a similare but different question I have..

Any help on the below would be much appreciated. (Wish we could record macros in Outlook. Just saying.)

I believe I will want to use something like the boiler template model explained but I haven't figured out how to implement that with items already in my sent mail folder.

I was also thinking an option could be to do a big VLOOKUP in a stored table based on the email address to pull in the corresponding person's name and report name.


I would like to drop mail items into a folder ("follow-up") and have the macro .ReplyAll to all the mail items in that folder and have a new dynamic (red font) body (black/red font) above the email chain (brown font).

Below is a sample sample of a follow-up email I would like to be automatically generated. I don't care if the email is in HTML or text style.In excel, I have the the name (Column A), report name (Column B), and email address (Column C).

_________________________________________________________

Tom,

Please see my below mail. You are the reason the TPS reports still have not been completed.Thank you,
MyName
__________________________________________

From: MyName
Sent: Friday, May 15, 2015 2:50 PM
To: CustomerName
Subject: Interest In CASCADE NUT AND BOLT

Tom,

Please complete the TPS reports so I can do my job.

Thank you,
MyName