How to Auto Update the Date in Subject when Forwarding or Replying an Email in Outlook

If you always want to ensure that the latest date is inserted into the subject no matter when forwarding or replying an email, you can use the way introduced in this post. It will auto update the date that has existed in the subject to the current date.

Auto Update the Date in Subject when Forwarding or Replying an Email in OutlookBy the means introduced in my previous article – “How to Auto Insert Current Date into the Subjects of Incoming Emails with Outlook VBA”, each incoming email’s subject will always contain a date. In this case, when you forward or reply this email, by default, the date will be kept in subject as well. However, at times, the existing date isn’t same as the date when you forward or reply. Thus, you may need to manually change the date. It may be troublesome. So, here we will expose a piece of VBA code, which will auto update the date no matter when forwarding or replying.

Auto Update the Date in Subject when Forwarding or Replying an Email

  1. To start with, launch your Outlook application.
  2. Then press “Alt + F11” key buttons in the main Outlook window.
  3. In the next “Microsoft Visual Basic for Applications” window, you ought to find and open the “ThisOutlookSession” project.
  4. Subsequently, copy and paste the following VBA code into this project.
Public WithEvents objExplorer As Outlook.Explorer
Public WithEvents objInspectors As Outlook.Inspectors
Public WithEvents objMail As Outlook.MailItem

Private Sub Application_Startup()
    Set objExplorer = Outlook.Application.ActiveExplorer
    Set objInspectors = Outlook.Application.Inspectors
End Sub

Private Sub objExplorer_SelectionChange()
    On Error Resume Next
    If objExplorer.Selection.Item(1).Class = olMail Then
       Set objMail = objExplorer.Selection.Item(1)
    End If
End Sub

Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
    If Inspector.CurrentItem.Class = olMail Then
       Set objMail = Inspector.CurrentItem
    End If
End Sub

Private Sub objMail_Forward(ByVal Forward As Object, Cancel As Boolean)
    Forward.Subject = SubjectWithUpdatedDate(Forward.Subject)
End Sub

Private Sub objMail_Reply(ByVal Response As Object, Cancel As Boolean)
    Response.Subject = SubjectWithUpdatedDate(Response.Subject)
End Sub

Private Sub objMail_ReplyAll(ByVal Response As Object, Cancel As Boolean)
    Response.Subject = SubjectWithUpdatedDate(Response.Subject)
End Sub

Function SubjectWithUpdatedDate(ByVal strSubject As String) As String
    Dim objRegExp As RegExp
    Dim objMatchCollection As MatchCollection
    Dim objMatch As match
    Dim strDate, strToday As String

    Set objRegExp = New RegExp
    'Format - "YYYY-MM-DD", such as "2017-06-02"
    objRegExp.Pattern = "\d\d\d\d-\d\d-\d\d"
    objRegExp.Global = True
 
    'Check if there is already such a date in the email subject
    If objRegExp.Test(strSubject) Then
       Set objMatchCollection = objRegExp.Execute(strSubject)
 
       For Each objMatch In objMatchCollection

           strDate = objMatch.value
           strToday = Format(Date, "YYYY-MM-DD")
 
           'If not same as today, replace it with today
           If strDate <> strToday Then
              SubjectWithUpdatedDate = Replace(strSubject, strDate, strToday)
           Else
              SubjectWithUpdatedDate = strSubject
           End If
       Next
    'If not contain a date, insert the current date
    Else
       SubjectWithUpdatedDate = strSubject & " (" & Format(Date, "YYYY-MM-DD") & ")"
    End If
End Function

VBA Code - Auto Update the Date in Subject when Forwarding or Replying an Email

  1. After that, you should sign this code.
  2. Later change your Outlook macro settings to enable digitally signed macros.
  3. Eventually, restart your Outlook application to activate the new VBA code.
  4. Now, you can have a try.
  • Firstly, select an email.
  • Then click “Reply”, “Reply All” or “Forward” button in the ribbon.Reply or Forward a Selected Mail
  • At once, a new email will pop up, in which you can see that the date in its subject line has been updated to the current date.Updated Date in Subject

If Confronted with Frequent Outlook Errors

It is inevitable that you will encounter various problems in Outlook, like abrupt restarting, sudden crash, and random PST errors and so on. Thereby, you have to keep a potent repair tool nearby, such as DataNumen Outlook Repair. It is able to fix PST errors and corruption within minutes and like a breeze.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including corrupt mdf and outlook repair software products. For more information visit www.datanumen.com

2 responses to “How to Auto Update the Date in Subject when Forwarding or Replying an Email in Outlook”

  1. Hello,
    When I run the script it gets tied up at objRegExp.
    A few threads indicated that Outlook 2010 does not recognize Regular Expressions. Is this tested on 2007?

    I’m no VBS expert but I’ve been working my way through this functionality.

    Mike

    • I’ve turned on reference tool for Microsoft VBScript Regular Expressions 5.5 and still no success. Script does not update subject line.