{"id":54082,"date":"2023-08-12T10:31:44","date_gmt":"2023-08-12T09:31:44","guid":{"rendered":"https:\/\/ekiwi-blog.de\/54082\/vba-outlook-automatically-move-e-mail-to-another-folder-with-macro\/"},"modified":"2023-08-12T10:38:51","modified_gmt":"2023-08-12T09:38:51","slug":"vba-outlook-automatically-move-e-mail-to-another-folder-with-macro","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/54082\/vba-outlook-automatically-move-e-mail-to-another-folder-with-macro\/","title":{"rendered":"VBA Outlook macro: Automatically move e-mail to another folder"},"content":{"rendered":"<p>To simplify and speed up the work with many mails, it is advisable to insert a few automatisms. This is the case, for example, if you receive a lot of emails of a certain type that you want to automatically assign to a certain subfolder.<br \/>\n<strong>Example 1:<\/strong> You get a lot of emails from your online shop with incoming orders and you want them to be automatically moved to a folder called &#8220;<em>Orders<\/em>&#8220;.<br \/>\n<strong>Example 2:<\/strong> You simply want to move certain selected emails from the inbox to an archive folder with one click.<\/p>\n<p>Of course, you can also use Outlook&#8217;s built-in functions for creating automatic rules. The following screenshot shows where to find them.<\/p>\n<figure id=\"attachment_54067\" aria-describedby=\"caption-attachment-54067\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-54067 size-full\" title=\"Possible alternative to VBA code to move a mail to another Outlook folder\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/08\/outlook-create-rule.jpg\" alt=\"Screenshot of Outlook how to create a rule\" width=\"650\" height=\"382\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/08\/outlook-create-rule.jpg 650w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/08\/outlook-create-rule-300x176.jpg 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><figcaption id=\"caption-attachment-54067\" class=\"wp-caption-text\">Create rule for moving an email to subfolder<\/figcaption><\/figure>\n<p>However, there are certainly use cases where this is not quite the appropriate tool of first choice, e.g. if you want to banish all marked emails to a certain subfolder with one click. Or if you already have another <a href=\"https:\/\/ekiwi-blog.de\/en\/49256\/vba-macro-automatically-add-a-file-attachment-to-an-e-mail-message\/\" title=\"VBA macro: Automatically add a file attachment to an e-mail message\">VBA macro<\/a>, e.g. for automatically replying to an email, and in the same macro you want to <a title=\"how to mark an outlook email as read using VBA\" href=\"https:\/\/ekiwi-blog.de\/en\/53092\/vba-outlook-mark-e-mail-as-read\/\">mark the source mail as read<\/a> and then move it from the inbox somewhere else.<\/p>\n<h2>VBA code: Move email to subfolder<\/h2>\n<p>The following screenshot and code example show how to move one or more selected emails to a subfolder of the Inbox.<\/p>\n<figure id=\"attachment_54079\" aria-describedby=\"caption-attachment-54079\" style=\"width: 700px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-54079 size-full\" title=\"Automatically move Outlook mail to another folder\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/08\/vba-code-move-outlook-email-to-another-folder.jpg\" alt=\"VBA editor screenshot of the VBA code to move an email to a subfolder or archive folder.\" width=\"700\" height=\"265\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/08\/vba-code-move-outlook-email-to-another-folder.jpg 700w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/08\/vba-code-move-outlook-email-to-another-folder-300x114.jpg 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-54079\" class=\"wp-caption-text\">VBA-Code: Move email to subfolder<\/figcaption><\/figure>\n<p>This code example loops through all the emails selected in Outlook in the <em>For Each<\/em> loop and then moves them to a specified folder (<em>&#8220;All Mails&#8221;<\/em>), which is a subfolder of the Inbox.<\/p>\n<pre><code><span style=\"color: #0000ff;\">Public Sub<\/span> MoveMail()\r\n    <span style=\"color: #0000ff;\">Dim<\/span> olItem <span style=\"color: #0000ff;\">As<\/span> Outlook.MailItem\r\n    <span style=\"color: #0000ff;\">Dim<\/span> objNS <span style=\"color: #0000ff;\">As<\/span> Outlook.NameSpace\r\n    <span style=\"color: #0000ff;\">Dim<\/span> objFolder <span style=\"color: #0000ff;\">As<\/span> Outlook.MAPIFolder\r\n    <span style=\"color: #0000ff;\">Dim<\/span> destFolder <span style=\"color: #0000ff;\">As<\/span> Outlook.Folder\r\n    \r\n    <span style=\"color: #339966;\">'Define the destination folder; in this case it is \"All Mails\", a subfolder of \"Inbox\".<\/span>\r\n    <span style=\"color: #0000ff;\">Set<\/span> objNS = GetNamespace(\"MAPI\")\r\n    <span style=\"color: #0000ff;\">Set<\/span> objFolder = objNS.Folders.GetFirst <span style=\"color: #339966;\">' folder of the current mailbox<\/span>.\r\n    <span style=\"color: #0000ff;\">Set<\/span> objFolder = objFolder.Folders(\"Inbox\")\r\n    <span style=\"color: #0000ff;\">Set<\/span> destFolder = objFolder.Folders(\"All Mails\")\r\n    \r\n   <span style=\"color: #339966;\"> <span style=\"colour: #339966;\">'Move all selected emails to the defined subfolder<\/span>.<\/span>\r\n    <span style=\"color: #0000ff;\">For Each<\/span> olItem <span style=\"color: #0000ff;\">In<\/span> Application.ActiveExplorer.Selection\r\n        olItem.Move destFolder\r\n    <span style=\"color: #0000ff;\">Next<\/span> olItem\r\n<span style=\"color: #0000ff;\">End Sub\r\n<\/span><\/code><\/pre>\n<p>To get the object for the subfolder, you first have to define the Mapi namespace (<em>GetNamespace(&#8220;MAPI&#8221;)<\/em>). There you determine the relevant mailbox with <em>objNS.Folders.GetFirst<\/em>. We use <em>GetFirst<\/em> here because the relevant mailbox is in the first place in Outlook. If you have several mailboxes, you can either navigate to them with <em>GetNext<\/em> or address them directly via the index using <em>.Item(number)<\/em>.<\/p>\n<p>Then define the inbox as a folder and access its folder array to define the appropriate subfolder of the inbox. This is then the destination folder where the selected e-mails are to be moved to.<\/p>\n<p>Please note that for the code to work, various references must be activated under References.<\/p>\n<figure id=\"attachment_53075\" aria-describedby=\"caption-attachment-53075\" style=\"width: 445px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-53075\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/06\/vba-editor-necessary-references-for-outlook.jpg\" alt=\"Screenshot of the necessary references in the VBA editor\" width=\"445\" height=\"364\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/06\/vba-editor-necessary-references-for-outlook.jpg 445w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/06\/vba-editor-necessary-references-for-outlook-300x245.jpg 300w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><figcaption id=\"caption-attachment-53075\" class=\"wp-caption-text\">Set the required library under Extras \/ Links<\/figcaption><\/figure>","protected":false},"excerpt":{"rendered":"<p>To simplify and speed up the work with many mails, it is advisable to insert a few automatisms. This is<\/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":[1701,2249,2252,1855,1707,2737,3154,1857,1858,2168,3231,1700,1861,2715,1862,2068,1710],"class_list":["post-54082","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-access-en","tag-automatisch","tag-code-en","tag-excel-en-2","tag-macro","tag-macros","tag-makros-en","tag-microsoft-en-2","tag-microsoft-office-en-2","tag-office-en","tag-powerpoint-en","tag-programming","tag-vba-en-2","tag-vba-code-en","tag-visual-basic-for-application-en-2","tag-windows-en-2","tag-word-en"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/54082","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=54082"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/54082\/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=54082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=54082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=54082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}