Archiving Microsoft Outlook emails using VBA Code

Regularly archiving emails can keep your Outlook inbox organized and your important emails safe.

This blog post delves into how you can use Visual Basic for Applications (VBA) in Microsoft Outlook to automate the archiving process, making it a simple and efficient task.

The VBA Code for Email Archiving

Sub ArchiveEmails()
    Dim myNamespace As Outlook.NameSpace
    Dim myFolder As Outlook.Folder
    Dim myArchiveFolder As Outlook.Folder
    Dim myItems As Outlook.Items
    Dim myItem As Object

    Set myNamespace = Application.GetNamespace("MAPI")
    Set myFolder = myNamespace.GetDefaultFolder(olFolderInbox)
    Set myArchiveFolder = myNamespace.Folders("Archive").Folders("Inbox")
    Set myItems = myFolder.Items

    For Each myItem In myItems
        If TypeOf myItem Is Outlook.MailItem Then
            myItem.Move myArchiveFolder
        End If
    Next myItem
End Sub

Understanding the VBA Code

let’s break down this VBA (Visual Basic for Applications) code used in Microsoft Outlook for archiving emails.

The code automates the process of moving emails from your Inbox to an Archive folder.

Here’s a step-by-step explanation:

Start of the Subroutine:

Sub ArchiveEmails()

This line begins a subroutine named ArchiveEmails. A subroutine in VBA is a block of code that performs a specific task.

Declaring Variables:

Dim myNamespace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim myArchiveFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Object
  • This section declares variables to be used in the script.
  • myNamespace: An object representing the MAPI namespace, which is used for accessing folders in Outlook.
  • myFolder: This will represent the Inbox folder.
  • myArchiveFolder: This will represent the destination Archive folder.
  • myItems: A collection that will hold all items (emails) in the Inbox.
  • myItem: A generic object to represent each individual item (email) during the loop.

Setting Namespace and Folders:

Set myNamespace = Application.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(olFolderInbox)
Set myArchiveFolder = myNamespace.Folders("Archive").Folders("Inbox")
Set myItems = myFolder.Items
  • Set myNamespace: Assigns the MAPI namespace to myNamespace.
  • Set myFolder: Assigns the default Inbox folder to myFolder.
  • Set myArchiveFolder: Locates the Archive folder (assumed to be within the main namespace structure), specifically an “Inbox” folder within “Archive”.
  • Set myItems: Assigns all the items in the Inbox to myItems.

Looping Through and Archiving Emails:

For Each myItem In myItems
    If TypeOf myItem Is Outlook.MailItem Then
        myItem.Move myArchiveFolder
    End If
Next myItem
  • This loop iterates through each item in myItems (the Inbox).
  • If TypeOf myItem Is Outlook.MailItem Then: This checks if the item is an email (as opposed to other types of items like calendar entries).
  • myItem.Move myArchiveFolder: Moves the email to the specified Archive folder.
  • The loop continues until every item has been checked and applicable items have been moved.

End of the Subroutine:

End Sub

This line marks the end of the ArchiveEmails subroutine.

In summary, when this VBA script is run, it will automatically move all emails from your Inbox to a designated Archive folder, helping you manage your emails more efficiently by decluttering your Inbox.

How to Implement the VBA Code in Outlook

Open VBA Editor: In Outlook, press Alt + F11 to open the VBA editor or go to the Developer tab

Click on Visual Basic to open the VBA editor.

Insert a Module: Right-click on ThisOutlookSession, choose Insert, then Module.

Copy and Paste the Code: Enter the VBA script into the module.

Save and Exit: Save the module and close the editor.

Using the VBA Script

  1. Restart Outlook: To ensure the script is ready.
  2. Execute the Script: Press Alt + F8, select ArchiveEmails, and click ‘Run’.
  1. Check Archive Folder: Verify that the emails have been moved correctly.

Final Words

In conclusion, automating the archiving of emails in Outlook using VBA is a straightforward way to enhance your email management.

By implementing this method, you can maintain an organized inbox and protect your important emails without manual effort.

Give this VBA script a try and experience a more efficient and organized approach to handling your emails.

Related Articles