How to Reply to a selected Outlook Email with Pre-Defined Texts

agent_maxine

New Member
Joined
Aug 23, 2017
Messages
38
I found a code somewhere that enables users to ReplyAll to a selected email in MS Outlook. I would also like to add a pre-defined set of texts while keeping the history of email trail. I've seen this type of code quite often:
.HTMLBody = EmailBody & .HTMLBody

However I keep getting "Run-time error 287: Application-defined or object-defined error." on the ".HTMLBody = EmailBody & .HTMLBody" line.
What am I missing?

Code:
Sub ReplyAll_Attachments
'Create Email as ReplyAll
'Reference Required: VBE > Tools > References > Microsoft Outlook 15.0 Object Library

Dim outlookApp As Outlook.Application, outlookMail As Outlook.MailItem, outlookReply As Outlook.MailItem
Dim outlookItem As Object, EmailBody As String
    Set outlookApp = New Outlook.Application
    Set outlookMail = outlookApp.CreateItem(0)
    Set outlookItem = GetCurrentItem()

EmailBody = "Hello there"

If Not outlookItem Is Nothing Then
    Set outlookReply = outlookItem.ReplyAll
    With outlookReply
    .HTMLBody = EmailBody & .HTMLBody
    .Display
    outlookItem.UnRead = False
End With
End If

Set outlookReply = Nothing: Set outlookItem = Nothing: Set outlookMail = Nothing: Set outlookApp = Nothing

End Sub


Function GetCurrentItem() As Object

Dim objApp As Outlook.Application
    Set objApp = Outlook.Application

Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
        Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
    Case "Inspector"
        Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select

Set objApp = Nothing

End Function

Thank you in advance for any help!
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
What happens when you run the code below? Do you get explorer or inspector in the mail body? What Office version are you using?

Code:
' Excel module
Dim aw$
Sub ReplyAll_Attachments()
'Reference Required: VBE > Tools > References > Microsoft Outlook 15.0 Object Library
Dim outlookApp As Outlook.Application, outMail As MailItem, outReply As MailItem
Dim outlookItem As Object, EmailBody$
Set outlookApp = New Outlook.Application
Set outMail = outlookApp.CreateItem(0)
Set outlookItem = GetCurrentItem()
EmailBody = "Hello there " & aw
If Not outlookItem Is Nothing Then
    Set outReply = outlookItem.ReplyAll
    With outReply
        .BodyFormat = olFormatHTML
        .HTMLBody = EmailBody & .HTMLBody & vbLf & "<HTML>******>More text.</BODY></HTML>"
        .Display
        outlookItem.UnRead = False
    End With
End If
Set outReply = Nothing: Set outlookItem = Nothing
Set outMail = Nothing: Set outlookApp = Nothing
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Outlook.Application
Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
        Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        aw = "Explorer"
    Case "Inspector"
        Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
        aw = "Inspector"
End Select
Set objApp = Nothing
End Function
 
Upvote 0
Hi Worf, thank you for your reply.
I get Run-time error 287: Application-defined or object-defined error on the following line:
.HTMLBody = EmailBody & .HTMLBody & vbLf & "<HTML>******>More text.</BODY></HTML>"

Even when I comment out the above .HTMLBody line, I'm not sure if I get explorer or inspector in the mail body. How can I tell?
I am using Office/Excel/Outlook 2013, and MS Office/Excel/Outlook 15.0 Object Library.
 
Last edited:
Upvote 0
Hi


  • You cannot get an error on a comment.
  • You will get explorer or inspector when the code reaches the display line.
  • The forum software interpreted the tags, as it always does. Please try again with the corrected version below, replacing less with < and greater with >.



.HTMLBody = EmailBody & .HTMLBody & vbLf & "lessHTMLgreaterlessBODYgreaterMore text.less/BODYgreaterless/HTMLgreater"
 
Upvote 0
I still get the same Run-time error 287: Application-defined or object-defined error.
It debugs to this line:
.HTMLBody = EmailBody & .HTMLBody & vbLf & "<HTML>******>More text.</BODY></HTML>"

The issue is that it does not allow the ".HTMLBody = (Other) & .HTMLBody & (Other)" format.
If I remove ".HTMLBody" from the right side, it works -- I get Explorer in the mail body. Unfortunately, adding other texts before & after .HTMLBody is the easiest way I've seen to keep the email history when replying... But I can't seem to combine the two features.
 
Last edited:
Upvote 0
A different version for your testing. If this fails, we can try the Word editor.

Code:
' Excel module
Dim aw$
Sub ReplyAll_Attachments()
'Reference Required: VBE > Tools > References > Microsoft Outlook 15.0 Object Library
Dim outApp As Outlook.Application, outReply As MailItem
Dim outItem As Object, EmailBody$, ns As Namespace, sid$
Set outApp = New Outlook.Application
Set outItem = GetCurrentItem()
sid = outItem.EntryID
Set ns = outApp.GetNamespace("MAPI")
Set outItem = ns.GetItemFromID(sid)
EmailBody = "Hello there " & aw
If Not outItem Is Nothing Then
    Set outReply = outItem.ReplyAll
    With outReply
        .BodyFormat = olFormatHTML
        .HTMLBody = EmailBody & .HTMLBody & vbLf & "More text."
        .Display
        outItem.UnRead = False
    End With
End If
Set outReply = Nothing: Set outItem = Nothing: Set outApp = Nothing
End Sub
 
Upvote 0
Still getting the same Run-time error 287: Application-defined or object-defined error.
It does not like the ".HTMLBody = (Other) & .HTMLBody & (Other)" format... Do you know if it's an Office 2013 thing?
 
Upvote 0
It is not, since everything I posted worked for me with Office 2013. I will be back later, probably with something using the Word editor option.
 
Upvote 0
Another one, which also worked for me. Note that it is an Outlook macro.


Code:
' Outlook module
Dim aw
Sub ReplyWithOrig()
Dim objReply As MailItem, objDoc As Document, objSel As Word.Selection, ci As MailItem
Set ci = GetCurrentItem
Set objReply = ci.Reply
Set objDoc = objReply.GetInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
With objSel
    .Find.Execute "De:"             ' use From: here
    .Collapse wdCollapseStart
    .MoveEnd WdUnits.wdStory, 1
    .Copy
    .Move wdStory, 1
    .InlineShapes.AddHorizontalLineStandard
    .PasteAndFormat wdFormatOriginalFormatting
    .Move wdStory, -1
    .Find.ClearFormatting
    .Find.Execute ci.SenderName
End With
objDoc.Characters(1).InsertBefore "More text."
objReply.Display
Set objReply = Nothing:    Set objDoc = Nothing
End Sub

Function GetCurrentItem() As Object
Dim objApp As Application
Set objApp = Outlook.Application
Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
        Set GetCurrentItem = objApp.ActiveExplorer.Selection.item(1)
        aw = "Explorer"
    Case "Inspector"
        Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
        aw = "Inspector"
End Select
Set objApp = Nothing
End Function
 
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,796
Members
448,994
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top