Sometimes, you may want to compress all the attachments in an email into a zip file directly in Outlook. In this case, you can use the VBA code introduced in this article to achieve this feature.
By default, Outlook has its attachment size limit – 20 MB for POP3, IMAP or other web-based email accounts and 10 MB for Exchange accounts. So, when the total size of attachments exceeds the limit, you will get related errors. In this case, it is a good option to compress all the attachments into a zip file.
However, as you can see, Outlook doesn’t offer such a direct feature. That is to say, you need to first zip these files in hard drive and then re-attach the newly created zip file. In reality, most users hope that they can directly achieve it within Outlook. In response to this requirement, we will introduce how to use VBA to realize it in the followings.
Compress All Attachments into a Zip File
- In the first place, launch your Outlook application.
- Then press “Alt + F11” key buttons in the main Outlook window.
- Next in the new Outlook VBA editor window, open a module.
- Subsequently, copy and paste the following VBA codes into the module.
Sub ZipAttachments() Dim objMail As Outlook.MailItem Dim objAttachments As Outlook.attachments Dim objAttachment As Outlook.Attachment Dim objFileSystem As Object Dim objShell As Object Dim varTempFolder As Variant Dim varZipFile As Variant 'Save the attachments to Temporary folder Set objFileSystem = CreateObject("Scripting.FileSystemObject") varTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp " & Format(Now, "dd-mm-yyyy- hh-mm-ss-") MkDir (varTempFolder) varTempFolder = varTempFolder & "\" Set objMail = Outlook.Application.ActiveInspector.CurrentItem Set objAttachments = objMail.attachments For Each objAttachment In objAttachments objAttachment.SaveAsFile (varTempFolder & objAttachment.FileName) Next 'Create a new zip file varZipFile = InputBox("Specify a name for the new zip file", "Name Zip File", objMail.Subject) varZipFile = objFileSystem.GetSpecialFolder(2).Path & "\" & varZipFile & ".zip" Open varZipFile For Output As #1 Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0) Close #1 'Copy all the saved attachments to the new zip file Set objShell = CreateObject("Shell.Application") objShell.NameSpace(varZipFile).CopyHere objShell.NameSpace(varTempFolder).Items 'Keep macro running until Compressing is done On Error Resume Next Do Until objShell.NameSpace(varZipFile).Items.Count = objShell.NameSpace(varTempFolder).Items.Count Application.Wait (Now + TimeValue("0:00:01")) Loop On Error GoTo 0 'Delete all the attachments Set objAttachments = objMail.attachments While objAttachments.Count > 0 objAttachments.Item(1).Delete Wend 'Add the new zip file to the current email objMail.attachments.Add varZipFile 'Prompt MsgBox ("Complete!") End Sub
- After that, you need check your Outlook macro settings to ensure that macro is permitted.
- Later you can add the new VBA project to Quick Access Toolbar of message window as usual.
- Eventually you can have a try.
- Firstly, create a new email and attach several files. Or simply open a mail with many attachments.
- Next click the macro button in Quick Access Toolbar.
- Later you need to specify a name for the zip file and hit “OK”. By default, it will be set same as the mail subject.
- At once, all the attachments will be compressed into a zip file, like the following screenshot:
Unzip the .Zip Attachments Directly in Outlook
It is inevitable that you will receive any attachments with “.zip” file extension now and then. In this scenario, you may want to directly unzip such files from within your Outlook instead of saving and uncompressing them on the local drive. Even though Outlook doesn’t have such a native feature, you still can use VBA to get it as well. For more detail, you can refer to another article – “How to Unzip the .Zip Attachments Directly in Outlook via VBA”.
Author Introduction:
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including sql fix and outlook repair software products. For more information visit www.datanumen.com
1