In-line reply style in Outlook

Status
Not open for further replies.

Tomas

Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
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.
 
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
 
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
 
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.
 
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
 
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:
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.
 
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.
 
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.
 
- 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.
 
ok, did a very quick test and it worked on plain text messages.
 
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?
 
if you change name = oItem.SentOnBehalfOfName to name = oItem.Sendername does it work?
 
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.
 
You have word object model selected - plus itf it was a reference, the error would be different. I'll see if i can repro.
 
Hello Diane, thank you for the help. I'm just curious if you were able to figure this out?
 
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.
 
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?
 
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?
 
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?
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
N Line to move origEmail to subfolder within a reply macro Outlook VBA and Custom Forms 0
O On click,I want to change subject line of selected mail and then reply to particular email and move Using Outlook 3
J Remove extra line above signature in reply Outlook VBA and Custom Forms 5
M Appending text to the subject line of a Reply, Foward or new Email Outlook VBA and Custom Forms 2
H using VBA to edit subject line Outlook VBA and Custom Forms 0
O VBA - Regex - remove double line spacing Outlook VBA and Custom Forms 1
F Auto changing email subject line in bulk Using Outlook 2
R Outlook Working off line Using Outlook 0
D Prompt to prefix subject line whenever sending an email Outlook VBA and Custom Forms 3
Witzker Add a text line at the end of the note field in all selected Contacts Outlook VBA and Custom Forms 7
R 550 maximum allowed line length is 998 octets, got 1012 Using Outlook 7
2 How to get rid of the "Emailing:" prefix in the subject line Using Outlook 1
E Outlook - eliminate same adresses from to, and bc line Outlook VBA and Custom Forms 0
D ISOmacro to extract active mail senders name and email, CC, Subject line, and filename of attachments and import them into premade excel spread sheet Outlook VBA and Custom Forms 2
O Carriage Return - Line Feeds - exporting Calendar events Using Outlook 0
D cannot view emails in View pane (in the right pane), I only see one line or nothing Using Outlook 21
A Macro to file emails into subfolder based on subject line Outlook VBA and Custom Forms 1
V vBA for searching a cell's contents in Outlook and retrieving the subject line Outlook VBA and Custom Forms 1
C Macro to extract sender name & subject line of incoming emails to single txt file Outlook VBA and Custom Forms 3
B User defined field for messages with 'me' in the [To], [Cc] line Using Outlook 0
C How to rename subject line and forward the email Outlook VBA and Custom Forms 2
B Add Prefix text to Subject Line Using Outlook 1
M Convert Subject Line to Internet Header version of Subject Outlook VBA and Custom Forms 10
O Double line spacing - solved with minor drawback Using Outlook 0
J VBA Outlook : Subject line : Cut and Paste name to heading , number to very end of the body of Email Outlook VBA and Custom Forms 1
N Wide Line Spacing in Folder pane 2016 Using Outlook 18
C Change Subject Line in Selected Emails Outlook VBA and Custom Forms 1
O Adding a new account - "CompanyL (none)" line is added Using Outlook 5
C Outlook - cannot save subject line changes Using Outlook 2
N Select Appointment subject line from combobox or list Outlook VBA and Custom Forms 1
M Subject Line Automation - Trigger Script Delayed Outlook VBA and Custom Forms 2
R Sending email copy (*.msg file) of sent email if subject line contains specific string. Outlook VBA and Custom Forms 1
J Marco in search of text in subject line Using Outlook 8
N automatic response with an attachment based on the subject line Outlook VBA and Custom Forms 1
K ind specific Subject line from outlook and copy the content of the email body to exce Outlook VBA and Custom Forms 0
C replace subject line generated by converting a word document to PDF and sending it to an email Using Outlook 8
C Outlook Subject Line Macro Outlook VBA and Custom Forms 0
Harald Olsen Automated insertion of address in address line Using Outlook 1
P Can't see full text in From and Subject line Using Outlook 1
D dropdown list in new email in subject line Using Outlook 4
N Rename Subject Line with Email Content Outlook VBA and Custom Forms 3
Stilgar Relsik Create a rule to copy text from an email and paste it in the subject line. Using Outlook 1
P MS OUTLOOK 2013 - Adding Sender on the CC line Using Outlook 5
I Make a macro in line rather than pop out Outlook VBA and Custom Forms 0
Diane Poremsky Editing the Subject Line in Outlook Using Outlook 0
Aussie I Change the Subject Line ... but after it is moved the subject has reverted Using Outlook 1
Diane Poremsky Add a file number or keyword to the subject line of messages Using Outlook 0
O How to set subject line in replies using VBA Outlook VBA and Custom Forms 1
B VBA to prepend subject line Using Outlook 0
M Outlook adds strange characters inserted into the Subject Line Using Outlook 2

Similar threads

Back
Top