How to Unzip the .Zip Attachments Directly in Outlook via VBA

When you receive an attachment in “.zip” file extension, if you want to unzip them, in general, you should save and unzip them in local drive. But many hope to unzip them directly in Outlook. This article will teach you how to get it via VBA.

You must have ever received the attachments which are in “.zip” file extension. In order to access the internal files in one zip file, you have to firstly unzip it. In this case, as usual, you have to first save the zip file to local drive and then right click it and select “Extract All” from the context menu. In reality, many users prefer to unzip the file directly within Outlook. However, Outlook does not provide such a native feature. Fortunately, you can follow the steps thereinafter to utilize VBA to realize it like a breeze.

Unzip the .Zip Attachments Directly in Outlook via VBA

Unzip the .Zip Attachments Directly in Outlook

  1. In the first place, launch your Outlook application.
  2. Then press “Alt + F11” key shortcut in Outlook window to access VBA editor.
  3. Next in the “Microsoft Visual Basic for Applications” window, open a module that is not in use.
  4. Subsequently, copy and paste the following VBA codes into this module.
Public Sub UnzipFileInOutlook()
    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.attachments
    Dim objAttachment As Outlook.Attachment
    Dim objShell As Object
    Dim objFileSystem As Object
    Dim strTempFolder As String
    Dim strFilePath As String
    Dim strFileName As String
 
    Set objMail = Outlook.Application.ActiveInspector.CurrentItem
    Set objAttachments = objMail.attachments
 
    'Save & Unzip the zip file in local drive
    Set objShell = CreateObject("Shell.Application")
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    strTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp" & Format(Now, "yyyy-mm-dd-hh-mm-ss")
    MkDir (strTempFolder)
 
    For Each objAttachment In objAttachments
        If Right(objAttachment.FileName, 3) = "zip" Then
           strFilePath = strTempFolder & "\" & objAttachment.FileName
           objAttachment.SaveAsFile (strFilePath)
           objShell.NameSpace((strTempFolder)).CopyHere objShell.NameSpace((strFilePath)).Items
        End If
    Next
 
    'Reattach the files extracted from the zip file
    strFileName = Dir(strTempFolder & "\")
 
    While Len(strFileName) > 0
          objMail.attachments.Add (strTempFolder & "\" & strFileName)
          strFileName = Dir
          objMail.Save
    Wend
 
    'Delete the attachments in “.zip” file extension
    Set objAttachments = objMail.attachments
    For Each objAttachment In objAttachments
        If Right(objAttachment.FileName, 3) = "zip" Then
           objAttachment.Delete
           objMail.Save
        End If
    Next
 
    'Delete the temp folder and files
    objFileSystem.DeleteFolder (strTempFolder)
End Sub

VBA Code - Unzip the .Zip Attachments Directly in Outlook

  1. After that, you can add the new VBA project to the Quick Access Toolbar of message window as usual.
  2. Finally, you can have a try.
  • First, open a message with zip file in its own window.
  • Then click “Actions” > “Edit Message”.
  • Next click the new macro button in Quick Access Toolbar.
  • At once, the zip file will unzipped, like the following screenshot:Unzip File

Get Rid of Annoying PST Issues

Though the PST file in new Unicode format supports 20 GB file size, it’s still wise to keep file as small as possible. It is because that a large PST file is much more prone to corruption. Once your PST file gets compromised, you will have to make efforts to recover PST data. In that scenario, you have no choice but to recur to a well-proven and reputable tool like 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 sql repair and outlook repair software products. For more information visit www.datanumen.com

2 responses to “How to Unzip the .Zip Attachments Directly in Outlook via VBA”

  1. Hello, Thank you for making these instructions so easy to understand! However, I get an error message when I try to complete the action through the Quick Access bar. It will attach a second .zip folder to the email (not one with extracted documents), and a window pops up saying “Run-time error ‘-2147221239 (80040109) The function cannot be performed because the message has changed.” When I click “Debug,” it highlights “objMail.Save” under the “Reattach the files extracted from the zip file” section. Please let me know if there’s anything that I can do to fix this. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *