Manually Archiving Read Items


 

This post goes out to Richard Paine who sent me this question last week.

I want to move all the read emails from my inbox into my archive on a click of a button. I know that I can’t do this with the rules provided so have to use a function. Is there an easier way to do this?

Richard is correct that this cannot be done with rules. Of course he could use Outlook’s built-in auto archive function. The problem with auto archive is that it runs on a schedule, not on demand. The solution then is to use a script that Richard can run whenever he wants to.

As with many scripts, this one is very simple. It grabs the Inbox and Archive folders, then loops through the items in the inbox moving all those marked as read to the archive folder. When it finishes it will display a dialog-box letting Richard know it’s done. Like I said, simple stuff.

Requirements.

This solution should work in any version of Outlook.

Instructions.

Follow these instructions to add the code to Outlook.

  1. Start Outlook
  2. Press ALT + F11 to open the Visual Basic Editor
  3. If not already expanded, expand Microsoft Office Outlook Objects
  4. If not already expanded, expand Modules
  5. Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting InsertModule.
  6. Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  7. Click the diskette icon on the toolbar to save the changes
  8. Close the VB Editor
Sub ArchiveReadItems()
    'On the next line edit the path to your archive folder
    Const ARCHIVE_PATH = ""
    Const SCRIPT_NAME = "Archive Read Items"
    Dim olkInbox As Object, _
        olkArchive As Object, _
        olkItem As Object, _
        intCnt As Integer
    'Get the inbox folder
    Set olkInbox = Session.GetDefaultFolder(olFolderInbox)
    'Get the archive folder
    Set olkArchive = OpenOutlookFolder(ARCHIVE_PATH)
    'Loop through the items in the ibox and move those that have been read to the archive
    For intCnt = olkInbox.Items.Count To 1 Step -1
        Set olkItem = olkInbox.Items.Item(intCnt)
        If Not olkItem.UnRead Then
            olkItem.Move olkArchive
        End If
    Next
    'Clean-up
    Set olkInbox = Nothing
    Set olkArchive = Nothing
    Set olkItem = Nothing
    MsgBox "Archive process complete.", vbInformation + vbOKOnly, SCRIPT_NAME
End Sub

Function OpenOutlookFolder(strFolderPath)
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders, varFolder, bolBeyondRoot
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = olkSes.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

Adding Buttons to Run the Macro with a Single Click.

You can add toolbar buttons (Outlook 2003) or QAT buttons (Outlook 2007/2010) to run the macros with a single click. Here’s a link to a page that explaining how to do both.

Advertisements

5 comments on “Manually Archiving Read Items

  1. Hi David,

    I have tried to include the directory as you have described but still keep getting the error, I have even tried to create a new subfolder to the inbox but can’y get that to work either.

  2. hi David,

    Thanks for the solution and sorry for the delayed response, I have been away on business.

    I have loaded the script and tried to run it but get an errorr on the following line

    olkItem.Move olkArchive

    do you have to set up the archive with a destination address or does outlook use a default?

    • No worries, Richard. I hope the business went well.

      You have to provide a path, an Outlook path not a file path, to the archive file. If the archive doesn’t exist, then you’ll need to create it first. In case you aren’t familiar with paths in Outlook, here’s an explanation.

      A folder path in Outlook is essentially the same as a folder path in the file system. The one difference being that Outlook folder paths do not include a drive letter. The path to a folder is a list of all the folders from the root to the target folder with each folder name separated from the preceding folder name by a backslash (i.e. \). Consider the following folder structure:

      Mailbox – Doe, John
      – Calendar
      – Inbox
      – Tasks
      Personal Folders
      + Marketing
      + Proposals
      + Reviews
      + Projects
      + Project 1
      + Project 2

      The path to “Inbox” is “Mailbox – Doe, John\Inbox”.
      The path to “Reviews” is “Personal Folders\Marketing\Reviews”.
      The path to “Project 1” is “Personal Folders\Projects\Project 1”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s