{"id":806,"date":"2011-07-01T20:20:57","date_gmt":"2011-07-02T01:20:57","guid":{"rendered":"http:\/\/anvil-of-time.com\/wordpress\/?p=806"},"modified":"2021-03-10T20:38:11","modified_gmt":"2021-03-11T01:38:11","slug":"vba-removing-attachments-from-outlook-email-2","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/programming\/vba-removing-attachments-from-outlook-email-2\/","title":{"rendered":"VBA &#8211; Removing attachments from Outlook email"},"content":{"rendered":"<p>One very powerful aspect of the Microsoft Office Suite of applications is the ability to create Macros and Scripts to perform various processes.  This gives those of us who worked in Visual Basic 6 a chance to dust off our skills since the &#8216;flavor&#8217; of Visual Basic used in the Microsoft Office applications (aptly called Visual Basic for Applications) is essentially the same.<\/p>\n<p>A common request in Outlook is to process mail messages with attachments by removing the attachments and saving them to some specified folder.  This can be accomplished with either a macro or a script.  The script has an advantage of being something you can put into a rule so any incoming emails are processed automatically.  Macros allow you to process only specific emails should you so desire.<\/p>\n<p>My examples below are set to remove external calendar request files (.ICS or ICalendar format) and place them into the folder &#8216;ICSFiles&#8217; set up in the standard &#8216;My Documents&#8217; folder.<\/p>\n<pre name=\"code\" class=\"VB\">Public Sub SaveICSAttachments()\n&#039; to set this as a script the declaration would be\n&#039; Public Sub SaveICSAttachments(objitem As MailItem)\nDim objOL As Outlook.Application\n&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;\n&#039; the next line removed if set up as a script\nDim objitem As Outlook.MailItem &#039;Object\n&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;\nDim objAttachments As Outlook.Attachments\nDim objSelection As Outlook.Selection\nDim i As Long\nDim lCount As Long\nDim sFile As String\nDim sFolderpath As String\nDim sDeletedFiles As String\n\n&#039; Instantiate an Outlook Application object.\nSet objOL = Application\n\nDim objNs As Outlook.NameSpace\nSet objNs = objOL.Session\nDim objFolder As Outlook.MAPIFolder\n\n&#039; Get the path to your My Documents folder\nsFolderpath = CreateObject(&quot;WScript.Shell&quot;).SpecialFolders(16)\nOn Error Resume Next\n\n&#039; Get the collection of selected objects.\nSet objSelection = objOL.ActiveExplorer.Selection\n\n&#039; Set the Attachment folder.\n&#039; You could check for the existance of the folder and \n&#039; prompt the user if you want\nsFolderpath = sFolderpath &amp; &quot;\\ICSFiles\\&quot;\n\n&#039; Check each selected item for attachments.\n&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;\n&#039; Remove loop if used in script (you call script with the item)\nFor Each objitem In objSelection\n&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;\n\t&#039; Remove this check if running as a script\n\t&#039; This code only strips attachments from mail items.\n\tIf objitem.Class = olMail Then\n\t&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;\n\t\t&#039; Get the Attachments collection of the item.\n\n\t\tSet objAttachments = objitem.Attachments\n\t\tlCount = objAttachments.Count\n\n\t\tIf lCount &gt; 0 Then\n\t\t\t&#039; Since we are removing things from an array, remove\n\t\t\t&#039; from the end going to the beginning\n\t\t\tFor i = lCount To 1 Step -1\n\n\t\t\t\t&#039; Save attachment before deleting from item.\n\t\t\t\tsFile = objAttachments.Item(i).FileName\n\t\t\t\t&#039; only looking for ICS file attachments\n\t\t\t\tIf Right(sFile, 3) = &quot;ics&quot; Then\n\t\t\t\t\t&#039; Combine with the path to the Temp folder.\n\t\t\t\t\tsFile = sFolderpath &amp; sFile\n\n\t\t\t\t\t&#039; Save the attachment as a file.\n\t\t\t\t\tobjAttachments.Item(i).SaveAsFile sFile\n                                        &#039; remove attachment\n\t\t\t\t\tobjAttachments.Item(i).Delete\n\n\t\t\t\t\t&#039;write the save as path to a string to add to the message\n\t\t\t\t\tIf objitem.BodyFormat &lt;&gt; olFormatHTML Then\n\t\t\t\t\t\tsDeletedFiles = sDeletedFiles &amp; vbCrLf &amp; &quot;&lt;file:\/\/&quot; &amp; sFile &amp; &quot;&gt;&quot;\n\t\t\t\t\tElse\n\t\t\t\t\t\tsDeletedFiles = sDeletedFiles &amp; &quot;&lt;br&gt;&quot; &amp; &quot;&lt;a href=&#039;file:\/\/&quot; &amp; sFile &amp; &quot;&#039;&gt;&quot; &amp; sFile &amp; &quot;&lt;\/a&gt;&quot;\n\t\t\t\tEnd If\n\t\t\tNext i\n\t\tEnd If\n\n\t\t&#039; Adds the filename string to the message body and save it\n\t\tIf objitem.BodyFormat &lt;&gt; olFormatHTML Then\n\t\t\t objitem.Body = objitem.Body &amp; vbCrLf &amp; &quot;The file(s) were saved to &quot; &amp; sDeletedFiles\n\t\t Else\n\t\t\t objitem.HTMLBody = objitem.HTMLBody &amp; &quot;&lt;p&gt;&quot; &amp; &quot;The file(s) were saved to &quot; &amp; sDeletedFiles &amp; &quot;&lt;\/p&gt;&quot;\n\t\t End If\n\t\t &#039; save the modified email with link to the detached file\n\t\t objitem.Save\n\t&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;\n\t&#039; remove if running as a script\n   End If\n   &#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;\n&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;\n&#039; remove loop if running as a script\nNext\n&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;\nExitSub:\n\nSet objAttachments = Nothing\nSet objitem = Nothing\nSet objSelection = Nothing\nSet objOL = Nothing\nEnd Sub<\/pre>\n<p>(Sorry for the screwed up code listing but the file link tags are being improperly interpreted by the plugin I use).<\/p>\n<p>This macro was designed to run on Outlook 2007.  You will most likely have to change the security settings in Outlook to have it run (and sign the macro or whatever).  There is minimal (none) error checking in this code but then I&#8217;m not going to do it all for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One very powerful aspect of the Microsoft Office Suite of applications is the ability to create Macros and Scripts to perform various processes. This gives those of us who worked in Visual Basic 6 a chance to dust off our skills since the &#8216;flavor&#8217; of Visual Basic used in the Microsoft Office applications (aptly called&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,85],"tags":[56,16,48],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/806"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=806"}],"version-history":[{"count":29,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/806\/revisions"}],"predecessor-version":[{"id":1966,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/806\/revisions\/1966"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}