• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

In-line reply style in Outlook

Tomas

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
#1
Hello everyone,

to be able to use In-line reply style, I copied following VBA code into my Outlook (source: Matthijs van de Water - In-line reply style in Outlook

Sub ReplyAllPlain()

Dim app As New Outlook.Application
Dim exp As Outlook.Explorer
Set exp = app.ActiveExplorer
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim item As Outlook.MailItem

'Get MailItem based on EntryID, otherwise we'll get security warnings
strID = exp.Selection.item(1).EntryID
Set olNS = Application.GetNamespace("MAPI")
Set item = olNS.GetItemFromID(strID)

' Store name of the sender and date of sent message
Dim name As String
name = item.SentOnBehalfOfName
datestr = Format(item.SentOn, "DDD, MMM dd, yyyy at HH:mm:ss")

' ReplyToAll to this message in Plain formatting with > style
item.BodyFormat = olFormatPlain
item.Actions("Reply to All").ReplyStyle = olReplyTickOriginalText
Dim rply As Outlook.MailItem
Set rply = item.ReplyAll

' Rebuild original body:
' - Remove Outlook-style reply header
' - Get rid of auto-inserted signature (optionally move to end of message)
orgBody = rply.Body
pos = InStr(orgBody, ">") - 1
sig = Left(orgBody, pos)
myBody = Mid(orgBody, pos + 1)
b = 0
lines = Split(myBody, vbNewLine)
For Each myLine In lines
If b > 4 Then
newBody = newBody & myLine & vbNewLine
End If
b = b + 1
Next

' Put new body together
rply.Body = "On " & datestr & ", " & name & " wrote:" _
& vbNewLine & newBody & vbNewLine '& sig

rply.Display

item.Close olDiscard
End Sub

Unfortunatelly, this macro will be stopped with run time error 91: object variable or with block variable not set. I guess, this happens at the beginning of part
' Rebuild original body:
' - Remove Outlook-style reply header
' - Get rid of auto-inserted signature (optionally move to end of message)

Could you please check this code and correct it? Thank's a lot in advance.
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#2
it's works here. Step into it and see which line it fails with.

Do you need plain text format or that reply format? I have a macro that always replies in a specific format, but it uses the Outlook format for the replies - with the header block and your designated reply style (>, etc) Always Reply Using HTML Format in Outlook
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#3
This tweak of my code will use > quotes, regardless of your outlook settings.
Header will look like this:

> -----Original Message-----
> From: EMO [mailto:emo@slipstick.com]
> Sent: Friday, November 18, 2016 3:14 AM
> To: granny@mobilegranny.com
> Subject: Exchange Messaging Outlook: Goodbye RPC over HTTP
>
> You're receiving this message because you signed up for Exchange
> Messaging Outlook

your macro uses
On Fri, Nov 18, 2016 at 03:14:18, EMO wrote:
>
> You're receiving this message because you signed up for Exchange

(if i have time tonight, i will tweak mine to use that format - it's just a matter of copying some lines over to the code that makes the conversion with all replies. )


Code:
Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)

   If bDiscardEvents Or oItem.BodyFormat = olFormat Then
       Exit Sub
   End If
   
   Cancel = True

   bDiscardEvents = True
  
' these two lines to add > to plain text reply
oItem.BodyFormat = olFormatPlain
oItem.Actions("Reply").ReplyStyle = olReplyTickOriginalText
   
   Dim oResponse As MailItem
   Set oResponse = oItem.Reply
   oResponse.BodyFormat = olFormat
   oResponse.Display
   
   bDiscardEvents = False

'close the message you are replying to
oItem.Close olDiscard
End Sub
 

Tomas

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
#4
Hello Diane,

thank you for your quick reply. It is not necessary to have plain text format in reply; I would only like to have the specific reply header "On Fri, Nov 18, 2016 at 03:14:18, EMO wrote:"
The last successful row in macro from Matthijs (yellow marked by Step Into - F8) is by me
item.Actions("Reply to All").ReplyStyle = olReplyTickOriginalText
After that I receive this failure massage with code 91.
(I have put this macro into Projekt1 (VbaProject.OTM) -> Microsoft Office Outlook Objects -> ThisOutlookSession)
Thank you again.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#5
changing my code to reply/forward automatically is fairly easy - but as written, it's always going to remove formatting - if you don't use the indent option in replies, it's possible to copy the original message and insert it into the reply then add the reply header at the top - this would remove the signature too, as it copies it before the signature is added.


This is the code that removes the formatting (and the signature, which can be added back at the end) - it uses the code from your macro sample.

Code:
Option Explicit

Private WithEvents oExpl As Explorer
Private WithEvents oItem As MailItem

Private bDiscardEvents As Boolean
Private olFormat As OlBodyFormat

Private Sub Application_Startup()
   
   Set oExpl = Application.ActiveExplorer
   
   bDiscardEvents = False
   
End Sub

Private Sub oExpl_SelectionChange()

   On Error Resume Next
   Set oItem = oExpl.Selection.Item(1)

End Sub

' reply all and forward use code similar to the macro below
Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
Dim datestr As String
Dim orgBody As String
Dim myBody As String
Dim newBody As String
Dim name As String
Dim pos
Dim b
Dim Lines
Dim myLine

   
   Cancel = True

   bDiscardEvents = True
name = oItem.SentOnBehalfOfName
datestr = Format(oItem.SentOn, "DDD, MMM dd, yyyy at HH:mm:ss")
newBody = oItem.Body


   Dim oResponse As MailItem
   Set oResponse = oItem.Reply
  
' - Remove Outlook-style reply header
orgBody = oResponse.Body
pos = InStr(orgBody, ">") - 1
sig = Left(orgBody, pos)
myBody = Mid(orgBody, pos + 1)
b = 0
Lines = Split(myBody, vbNewLine)
For Each myLine In Lines
If b > 4 Then
newBody = newBody & myLine & vbNewLine
End If
b = b + 1
Next
  
' Put new body together
oResponse.Body = "On " & datestr & ", " & name & " wrote:" _
& vbNewLine & newBody & vbNewLine '& sig

   oResponse.Display
   
   bDiscardEvents = False
  
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#6
ok... this is a more compact version of the code - it doesn't change the format and uses a different reply header -

this works for all messages.

It uses the word object model, so you'll need to set a reference to Word in Tools, References.



replies will look like this with html or plain text format -
reply-sample.png



Code:
Option Explicit
Private WithEvents oExpl As Explorer
Private WithEvents oItem As MailItem
Private bDiscardEvents As Boolean
Dim oResponse As MailItem
 
Private Sub Application_Startup()
   Set oExpl = Application.ActiveExplorer
   bDiscardEvents = False
End Sub
 
Private Sub oExpl_SelectionChange()
   On Error Resume Next
   Set oItem = oExpl.Selection.Item(1)
End Sub
 
' Reply
Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True
Set oResponse = oItem.Reply
 afterReply
End Sub
Private Sub oItem_Forward(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True
Set oResponse = oItem.Forward
 afterReply
End Sub
Private Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True
Set oResponse = oItem.ReplyAll
 afterReply
End Sub
Private Sub afterReply()
   
Dim datestr As String
Dim name As String
name = oItem.SentOnBehalfOfName
datestr = Format(oItem.SentOn, "DDD, MMM dd, yyyy at HH:mm:ss")
    Dim objWord As Word.Application
    Dim objInsp As Inspector
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    On Error Resume Next
            Set objInsp = oItem.GetInspector
            If objInsp.EditorType = olEditorWord Then
                Set objDoc = objInsp.WordEditor
                Set objWord = objDoc.Application
                Set objSel = objWord.Selection
        With objSel
             .WholeStory
             .Copy
       End With
            End If
  
With oResponse
  .Display
  .Body = ""
 Set objInsp = oResponse.GetInspector
 Set objDoc = objInsp.WordEditor
 Set objSel = objDoc.Windows(1).Selection
    With objSel
      .Paste
      .HomeKey Unit:=wdStory
      .InsertParagraphBefore
      .Font.name = "Calibri"
      .Font.Size = 12
      .Font.Color = wdBlack
      .ParagraphFormat.Alignment = wdAlignParagraphLeft
      .InsertBefore "On " & datestr & ", " & name & " wrote:" & vbCrLf
      .InsertParagraphBefore
      .HomeKey Unit:=wdStory
    End With
End With
    
End Sub
 
Last edited:

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#7
item.Actions("Reply to All").ReplyStyle = olReplyTickOriginalText
BTW - if you change this line to Reply, you need to change the Action to Reply
Set rply = item.ReplyAll
That should be the only cause of errors.
 

Tomas

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
#8
Hi Diane,

thank you for your code. I have only two additional questions:
- with word object model you mean Microsoft Word Object Library in Tools -> References?,
- is it possible to include in header also sender's email address?
-> After this change the header looks like this:
On Fri, Nov 18, 2016 at 03:14:18, EMO <emo@slipstick.com> wrote:

Thank you in advance.
 

Tomas

New Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
#9
Hi Diane,

do you have any idea why the macro (the last one with word object model) doesn´t work on plain text messages? Respectively it works. But original message will be completely removed. On HTML messages is everything perfect. Thank you in advance.
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#10
- is it possible to include in header also sender's email address?
Sure. You can do anything you want by changing either of these lines:
name = oItem.SentOnBehalfOfName
.InsertBefore "On " & datestr & ", " & name & " wrote:" & vbCrLf
to use oItem.senderemailaddress
- with word object model you mean Microsoft Word Object Library in Tools -> References?,
Yes, you need to use the word object 'library' - it's the object model by another name. :)

do you have any idea why the macro (the last one with word object model) doesn´t work on plain text messages?
offhand, no. It might not be copying the original message because it is plain text or insert before is wiping it out. (adding attachments has been known to clear the body.) Not sure if i'll get a chance to test it as i have a full schedule most of the week.
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#12
Dim datestr As String
Dim name As String
name = oItem.SentOnBehalfOfName
Hello. Sorry first post, and sorry for responding to an old thread, but I was trying the last bit of code, and I keep getting "Run-time error '91': Object variable or With block variable not set". When I use f8 and step through, or use breaks, it's always at the name = oItem.SentOnBehalfOfName. This is with Outlook 2016.

Any ideas?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#13
if you change name = oItem.SentOnBehalfOfName to name = oItem.Sendername does it work?
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#14
Sorry for some reason I didn't get a notification. No I still get the same thing.

Could it be something I missed in Tools, References? What all do you have checked? Do you have any other code or macros?

1523642531656.png


What I got.
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#15
You have word object model selected - plus itf it was a reference, the error would be different. I'll see if i can repro.
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#16
Hello Diane, thank you for the help. I'm just curious if you were able to figure this out?
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#17
I'm not able to repro this -
"Run-time error '91': Object variable or With block variable not set". When I use f8 and step through, or use breaks, it's always at the name = oItem.SentOnBehalfOfName. This is with Outlook 2016.
it works fine using the code in post #6. I am getting an error with plain text messages on the font settings, this is because plain text doesn't do fonts and formatting.

Did you change any of the variable names? if you change oitem to something else in the other modules, you need to change it everywhere.
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#18
No, I just copy/paste. Would you want to schedule a WebEx or similar screen sharing and you could take a look maybe if you wouldn't mind?
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#19
ok... this is a more compact version of the code - it doesn't change the format and uses a different reply header -

this works for all messages.

It uses the word object model, so you'll need to set a reference to Word in Tools, References.


replies will look like this with html or plain text format -
View attachment 1831


Code:
Option Explicit
Private WithEvents oExpl As Explorer
Private WithEvents oItem As MailItem
Private bDiscardEvents As Boolean
Dim oResponse As MailItem

Private Sub Application_Startup()
   Set oExpl = Application.ActiveExplorer
   bDiscardEvents = False
End Sub

Private Sub oExpl_SelectionChange()
   On Error Resume Next
   Set oItem = oExpl.Selection.Item(1)
End Sub

' Reply
Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True
Set oResponse = oItem.Reply
afterReply
End Sub
Private Sub oItem_Forward(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True
Set oResponse = oItem.Forward
afterReply
End Sub
Private Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
   Cancel = True
   bDiscardEvents = True
Set oResponse = oItem.ReplyAll
afterReply
End Sub
Private Sub afterReply()
 
Dim datestr As String
Dim name As String
name = oItem.SentOnBehalfOfName
datestr = Format(oItem.SentOn, "DDD, MMM dd, yyyy at HH:mm:ss")
    Dim objWord As Word.Application
    Dim objInsp As Inspector
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    On Error Resume Next
            Set objInsp = oItem.GetInspector
            If objInsp.EditorType = olEditorWord Then
                Set objDoc = objInsp.WordEditor
                Set objWord = objDoc.Application
                Set objSel = objWord.Selection
        With objSel
             .WholeStory
             .Copy
       End With
            End If
 
With oResponse
  .Display
  .Body = ""
Set objInsp = oResponse.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
    With objSel
      .Paste
      .HomeKey Unit:=wdStory
      .InsertParagraphBefore
      .Font.name = "Calibri"
      .Font.Size = 12
      .Font.Color = wdBlack
      .ParagraphFormat.Alignment = wdAlignParagraphLeft
      .InsertBefore "On " & datestr & ", " & name & " wrote:" & vbCrLf
      .InsertParagraphBefore
      .HomeKey Unit:=wdStory
    End With
End With
   
End Sub
Could it be because the private sub isn't declaring any variables in the parenthesis?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#20
No, not likely. It worked here... so its working. Are you still getting run time error 91: object variable or with block variable not set?