How to Auto Insert Current Date or Time into Email Subject or Body with Outlook VBA

Many users are used to inserting the current date or time into the email subject or body when composing a new email. This article will teach you how to achieve it in quick time.

In the previous post – “How to Auto Add Current Date to the Subjects of Your Outlook Emails”, you can learn how to auto add the current date or time to the subject. But the method in that post is simply to create a custom form of message. Thus, by that means, every time you want to create a new email with the current date or time in subject, you have to choose the custom form instead of creating a new email as usual. Obviously, it will be quite cumbersome.

Actually, Outlook has already offered us a native field to insert date or time. Also, you can apply Outlook VBA to auto insert the current date or time into the new email’s body or subject. As for which method you select, it depends on how often you need to insert the date or time.

Manually Insert by “Date & Time” Field

  1. To start with, launch Outlook.
  2. Then create a new message by clicking “New Email” button in “New” group under the “Home” tab.
  3. Subsequently, in the New Message window, you can click into the email body and turn to “Insert” tab.
  4. Next you can find and click the “Date & Time” field in “Text” group.Find Date & Time Field
  5. After that, a new dialog box will show up. You can choose an available format and then click “OK” button.Select Format for Date and Time
  6. After returning to the message window, you can see the current date or time in the email body. By default, Outlook doesn’t permit you to apply the “Date & Time” field to the subject.Current Date or Time in Message Body
  7. Hence, finally, if you want to insert the date or time to subject, you have to copy them from the body and paste them to the subject manually.

This method is more recommended if you seldom need to insert the current date or time. But provided that you frequently or always need to insert, you had better use the following VBA codes to achieve it automatically.

Auto Insert with Outlook VBA

  1. In the first place, press “Alt + F11” key buttons to access VBA editor.
  2. Then double click the “ThisOutlookSession” project on the left side to open it.
  3. Next copy the following codes into it.
Public WithEvents objInspectors As Inspectors
Public WithEvents objMail As MailItem

Private Sub Initialize_handlers()
    Set objInspectors = Application.Inspectors
End Sub

Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
    If TypeOf Inspector.CurrentItem Is MailItem Then
       Set objMail = Inspector.CurrentItem
    End If
End Sub

Private Sub objMail_Open(Cancel As Boolean)
    Dim strTime As String
 
    'the current time
    strTime = Now
 
    'insert to subject
    'If you don't want to insert to subject, remove the following line
    objMail.Subject = strTime
    'insert to body
    'If you don't want to insert to body, remove the following line
    objMail.Body = strTime & objMail.Body
End Sub

VBA Codes - Auto Insert Current Date or Time into Email Subject or Body

Note: The above codes will work to auto insert the current time into the subject and body. If you want to insert the current date, replace the “objMail_Open” sub with the followings:

Private Sub objMail_Open(Cancel As Boolean)
    Dim strDate As String
 
    'the current date
    strDate = Date
 
    'insert to subject
    objMail.Subject = strDate
    'insert to body
    objMail.Body = strDate & objMail.Body
End Sub
  1. Subsequently, sign this code as usual.
  2. Later, restart Outlook to activate the macro.
  3. Finally you can have a try.
  • Directly click on the “New Email” button to open a new email.New Email
  • You will find that the new email auto contains the current date or time in the subject and body, like the image below:Current Date or Time in Message Subject or Body

Don’t Trust in Unknown Emails Readily

You must have ever received junk emails. How do you usually deal with them? It is inadvisable to download attachments or open links in this kind of mails in that they may contain viruses. Once your Outlook data gets infected, the inbox repair tool, Scanpst.exe won’t make effects. In this case, you have no choice but to resort to a more experienced PST fix tool, such as DataNumen Outlook Repair.

Author Introduction:

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

Comments are closed.