How to Quickly Compress All Attachments into a RAR File in Your Outlook Email

Though Outlook doesn’t permit directly compressing attachments into a RAR file, you still can use the VBA code introduced in this article to realize it like a breeze.

As we all know, when attaching many large files to one email, you will be likely to get a warning about the attachment size limit in Outlook. Also, if an email carries too large attachments, it will become more difficult to send it out successfully. So, in face of such attachments, you can opt to compress them into a RAR file, which can reduce the size of the attached files in some degree.

Quickly Compress All Attachments into a RAR File in Your Outlook Email

In general, to compress files into a RAR file, you can only achieve it in local drive in that Outlook doesn’t provide such a native feature for compressing. However, many hope to accomplish it directly within Outlook. Therefore, here we will teach you how to get it with Outlook VBA.

Quickly Compress All Attachments into a RAR File in Your Outlook Email

  1. At the very outset, launch your Outlook application.
  2. Then press “Alt + F11” keys in the main Outlook window.
  3. Next you’ll enter “Microsoft Visual Basic for Applications” window. You need to open an empty module.
  4. Subsequently, copy and paste the following VBA code into this module.
Sub RarAttachments()
    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.attachments
    Dim objAttachment As Outlook.Attachment
    Dim objFileSystem As Object
    Dim objShell As Object
    Dim strTempFolder As Variant
    Dim strRARFile As Variant
    Dim strSourceFile As String
 
    'Save the attachments to Temporary folder
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    strTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp " & Format(Now, "YYYY-MM-DD hh-mm-ss")
    MkDir (strTempFolder)
 
    Set objMail = Outlook.Application.ActiveInspector.CurrentItem
    Set objAttachments = objMail.attachments
    For Each objAttachment In objAttachments
        objAttachment.SaveAsFile (strTempFolder & "\" & objAttachment.FileName)
    Next
 
    'Create a new RAR file
    strRARFile = InputBox("Specify a name for the new zip file", "Name Zip File", objMail.Subject)
    strRARFile = objFileSystem.GetSpecialFolder(2).Path & "\" & strRARFile & ".rar"
    Open strRARFile For Output As #1
    Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
    Close #1
 
    Set objShell = CreateObject("Shell.Application")
 
    'Add the files to the New RAR file
    strSourceFile = Dir(strTempFolder)
 
    While strSourceFile <> ""
          'Change "C:\Program Files (x86)\WinRAR\WinRAR.exe" to the location where your WinRAR is installed
          objShell.Run Chr(34) & "C:\Program Files (x86)\WinRAR\WinRAR.exe" & Chr(34) & " a -r" & Chr(34) & strRARFile & Chr(34) & " " & Chr(34) & strSourceFile & Chr(34)
          strSourceFile = Dir
    Wend
 
    'Delete all the attachments
    Set objAttachments = objMail.attachments
 
    While objAttachments.Count > 0
          objAttachments.Item(1).Delete
    Wend
 
    'Add the new RAR file to the current email
    objMail.attachments.Add strRARFile
 
    'Prompt you
    MsgBox "Complete!", vbExclamation
End Sub

VBA Code - Compress All Attachments into a RAR File

  1. After that, change your Outlook macro security level to low.
  2. Later, for convenient access, you can add the macro to Quick Access Toolbar.
  3. Eventually you can have a try.
  • First, open an email with many attachments.
  • Then press the macro button in Quick Access Toolbar.
  • Next you will need to input a name for the new RAR file and hit “OK”.
  • After that, all the original attachments will be replaced by a new RAR file like the screenshot:Effect: Compress All Attachments into a RAR File

Unrar .RAR Attachments in Outlook

Similarly, Outlook also doesn’t allow users to straightly decompress a .RAR file in it. Therefore, if you want to view the internal files in a .RAR attachment, you need to save and decompress it in your local drive. However, it is undoubtedly tedious. Fortunately, you still can utilize VBA to achieve this feature in Outlook, which has been introduced in my previous article “How to Unrar .RAR 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 Server corruption and outlook repair software products. For more information visit www.datanumen.com

Leave a Reply

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