If you always want to insert the current date into the subjects of incoming emails as they land into your local mailbox, you can utilize the VBA code introduced in this post.
My previous article “How to Auto Insert Current Date or Time into Email Subject or Body with Outlook VBA” has taught you to insert the current date into email subject when you’re composing an email. Similarly, many users wish that Outlook can auto insert the current date into the subject of incoming email as they arrive in their local mailbox. In response to this wish, we’ll introduce another way in the followings.
Auto Insert Current Date into the Subjects of Incoming Emails
- In the first place, start your Outlook application.
- Then in the main Outlook window, press “Alt + F11” key shortcuts.
- Next you will enter Outlook VBA editor window.
- Now you should open an empty module or insert a new one directly.
- Subsequently, copy and paste the following VBA code into such a module.
Sub InsertCurrentDateintoIncomingEmailSubjects(objMail As Outlook.MailItem) Dim strSubject As String Dim objRegExp As RegExp Dim objMatchCollection As MatchCollection Dim objMatch As match Dim strOriginalDate, strCurrentDate As String Set objRegExp = New RegExp 'Date in the format - "YYYY-MM-DD", such as "2017-06-01" objRegExp.Pattern = "\d\d\d\d-\d\d-\d\d" objRegExp.Global = True 'Check if the incoming email contains a date in "YYYY-MM-DD" format If objRegExp.Test(objMail.Subject) Then Set objMatchCollection = objRegExp.Execute(objMail.Subject) For Each objMatch In objMatchCollection strOriginalDate = objMatch.value strCurrentDate = Format(Date, "YYYY-MM-DD") 'Check if the date is same as current date 'if not same, replace the date with current date If strOriginalDate <> strCurrentDate Then objMail.Subject = Replace(objMail.Subject, strOriginalDate, strCurrentDate) 'if same, keep it Else objMail.Subject = objMail.Subject End If Next 'If not contain, insert the current date Else objMail.Subject = objMail.Subject & " (" & Format(Date, "YYYY-MM-DD") & ")" End If 'Save the changes on the Mail objMail.Save End Sub
- After that, you should sign this code and change your Outlook macro settings to allow signed macros.
- Later you should exit the VBA editor window.
- Then go to “File” menu and click on the “Manage Rules & Alerts” button.
- In the new dialog box, click on “New Rule”.
- Next in “Rule Wizard” screen, choose “Apply rule on messages I receive” and hit “Next”.
- In the subsequent screen, set conditions as per your needs and hit “Next”.
- After that, in “Actions” screen, select “run a script” and select the new script and click “OK” > “Next”.
- And then specify or ignore exception, and click “Next”.
- Lastly name the new rule. Now you will get a new rule like the image below:
- From now on, Outlook will auto insert the current date in the subjects of new emails as they arrive, shown as the following screenshot:
Outlook Errors Can Occur From Time to Time
Although Outlook provides users with a variety of features, it still cannot get rid of errors. In other words, you’ll encounter troubles in your Outlook now and then, such as sudden restarting or popup error messages and so on. To fix these issues, one of the best and more effective ways is to keep a preeminent repair tool handy, like DataNumen Outlook Repair. It is able to fix PST errors or corruption without breaking a sweat.
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including repair mdf and outlook repair software products. For more information visit www.datanumen.com