{"id":49287,"date":"2023-02-27T19:32:25","date_gmt":"2023-02-27T18:32:25","guid":{"rendered":"https:\/\/ekiwi-blog.de\/49287\/vba-get-date-and-time-from-outlook-e-mail\/"},"modified":"2023-02-27T19:40:34","modified_gmt":"2023-02-27T18:40:34","slug":"vba-get-date-and-time-from-outlook-e-mail","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/49287\/vba-get-date-and-time-from-outlook-e-mail\/","title":{"rendered":"VBA: Get date and time from Outlook e-mail"},"content":{"rendered":"<p><abbr title=\"Visual Basic for Application\">VBA<\/abbr> is very useful for automating daily, recurring tasks. If you use VBA in the context of Microsoft Outlook to simplify your daily work with e-mails or to transfer form data from e-mails to an Excel spreadsheet with one click, you may sometimes need to read out the date and\/or time when you received the e-mail.<\/p>\n<h2>VBA code<\/h2>\n<p>This is even relatively simple and can be done with just a few lines, once you have the mail object available in the VBA code. The corresponding Visual Basic code looks like this:<\/p>\n<pre><code><span style=\"color: #3366ff;\">Sub<\/span> TimeOfEMail()\r\n    <span style=\"color: #3366ff;\">Dim<\/span> objOL <span style=\"color: #3366ff;\">As<\/span> Outlook.Application\r\n    <span style=\"color: #3366ff;\">Dim<\/span> objMsg <span style=\"color: #3366ff;\">As<\/span> Outlook.MailItem\r\n    <span style=\"color: #3366ff;\">Dim<\/span> objSelection <span style=\"color: #3366ff;\">As<\/span> Outlook.Selection\r\n    <span style=\"color: #3366ff;\">Dim<\/span> receivedDateTime <span style=\"color: #3366ff;\">As<\/span> Date\r\n    <span style=\"color: #339966;\">' Outlook Application object<\/span>.\r\n    <span style=\"color: #3366ff;\">Set<\/span> objOL = CreateObject(\"Outlook.Application\")\r\n   <span style=\"color: #339966;\"> ' Get collection of selected objects (emails)<\/span>.\r\n    <span style=\"color: #3366ff;\">Set<\/span> objSelection = objOL.ActiveExplorer.Selection\r\n    <span style=\"color: #339966;\">'Use first selected email<\/span>.\r\n    <span style=\"color: #3366ff;\">Set<\/span> objMsg = objSelection.Item(1)\r\n    receivedDateTime = objMsg.ReceivedTime\r\n    <span style=\"color: #339966;\">'to something with DateTime Value<\/span>.\r\n    <span style=\"color: #3366ff;\">Debug.Print<\/span> receivedDateTime\r\n<span style=\"color: #3366ff;\">End Sub<\/span><\/code><\/pre>\n<p>The first part is just about finding the right VBA object that represents the email (<em>objMsg<\/em>). Once you have that, it&#8217;s quite simple. Because the <em>MailItem<\/em> object has a property called &#8220;<em>ReceivedTime<\/em>&#8220;, which contains the date and time. Via Debug.Print, it is output in this case in the console window or direct window.<\/p>\n<figure id=\"attachment_49283\" aria-describedby=\"caption-attachment-49283\" style=\"width: 699px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-49283\" src=\"http:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/02\/vba-outlook-get-time-of-email.jpg\" alt=\"Screenshot VBA editor with code to read the date and time of receipt of an email\" width=\"699\" height=\"535\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/02\/vba-outlook-get-time-of-email.jpg 699w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/02\/vba-outlook-get-time-of-email-300x230.jpg 300w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><figcaption id=\"caption-attachment-49283\" class=\"wp-caption-text\">Code and output in the console window<\/figcaption><\/figure>\n<p>If the standard output is not enough, you can format the date and time of receipt with the format function. This could then look like this:<\/p>\n<pre><code>MyDate = Format(receivedDateTime, \"dddd, mmm d yyyy\")\r\n<span style=\"color: #339966;\">' results in \"Monday, Feb 27 2023\"<\/span>.\r\n<span style=\"colour: #3366ff;\">Debug.Print<\/span> MyDate<\/code><\/pre>\n<p>Further and more detailed information on the different ways to output date and time formatted can be found in the <a href=\"https:\/\/learn.microsoft.com\/en-us\/office\/vba\/language\/reference\/user-interface-help\/format-function-visual-basic-for-applications\" target=\"_blank\" rel=\"noopener\">Microsoft documentation on the format function<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>VBA is very useful for automating daily, recurring tasks. If you use VBA in the context of Microsoft Outlook to<\/p>\n","protected":false},"author":2,"featured_media":6532,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[2714,2713,2252,1707,1703,1704,2168,1700,1705,2715,1706,1558],"class_list":["post-49287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-automate","tag-automatic","tag-code-en","tag-macro","tag-microsoft-en","tag-microsoft-office-en","tag-office-en","tag-programming","tag-vba-en","tag-vba-code-en","tag-visual-basic-for-application-en","tag-windows-en"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/49287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/comments?post=49287"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/49287\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/6532"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=49287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=49287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=49287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}