cancel
Showing results for 
Search instead for 
Did you mean: 

Restoring email from the Vault using VBA

Postman_Pat
Not applicable

Hi,

I needed to restore selected emails from the Vault and searched the Internet for some time to work out how to do it.  The Symantec API requires you to know ids which are stored as extended MAPI properties and cannot easily be accessed (Redemption may be an option if you know the GUID for the id).

Anyway I came up with a solution that allows the user to select the emails they want to restore, then the VBA opens each of these in turn and copies them to a predetermined folder.

A lot of the inspiration and code for this comes from this post http://stackoverflow.com/questions/17198895/outlook-2007-vba-macro-do-for-all-open-emails-and-move-to-a-folder.

Setting up the Developer toolbar

For those of you who have not used VBA in Outlook, you need to set up the Developer toolbar.  In Outlook 2010: go to File > Options > Customise Ribbon.  In the Right hand box check the "Developer" Option and click OK.  You should now see a Developer tab on the Ribbon.

Adding the VBA code

Click on the Developer tab.  You may need to create a Module to put your code in.  So right click the Module "folder" and Insert a Module.  If you have "Option Explicit" remove it then copy and paste the following code:

Sub UnarchiveToExportFolder()

   itemstoprocess = Outlook.ActiveExplorer.Selection.Count
   itemsmoved = 0
   If vbNo = MsgBox(itemstoprocess & " to process, do you want to proceed?", vbYesNo) Then GoTo theEnd
       
    Set myNameSpace = Application.GetNamespace("MAPI")
    Set myinbox = myNameSpace.GetDefaultFolder(olFolderInbox)
    Set myDestFolder = myinbox.Folders("Export")
 
    For Each olkMsg In Outlook.ActiveExplorer.Selection
    
        archive = (InStr(1, olkMsg.MessageClass, "IPM.Note", vbTextCompare) > 0)
        If archive Then
            olkMsg.Display
            Set myInspectors = Outlook.Application.ActiveInspector.CurrentItem
            Set myCopiedInspectors = myInspectors.Copy
            myCopiedInspectors.Move myDestFolder
            myInspectors.Close olDiscard
            
            Set myCopiedInspectors = Nothing
            
            itemsmoved = itemsmoved + 1
        End If
       
    Next
    
    Set olkMsg = Nothing
    Set myNameSpace = Nothing
    Set myinbox = Nothing
    Set myDestFolder = Nothing
    
theEnd:
    
    MsgBox ("Finished, items moved " & itemsmoved & ", items not moved " & itemstoprocess - itemsmoved & ".")
    
End Sub

Save the code

Click on the Save icon at the top left and then close the window.

Set up your Outlook folder

The code by default copies the emails to a folder within your Inbox called Export.  So create a New Folder called "Export" under your Inbox.

Try it out

Open the Developer tab.  Select an archived email and then click on Macros.  This will list all the Macros available to you.  The one you are after will called "XXXXXXXXX.UnarchiveToExportFolder".  The "XXXX"s will depend which Module you put your code into.

Select the Macro.  You will get a dialog telling you how many items it is about to process, confirming that you want to proceed.

Click on "Yes", it will then unarchive your email and copy it to the Export folder you created.

You will then get another dialog telling you how many it has processed.

Limitations

I will put my hands up to not being a professional programmer.  removing "Option Explicit" is lazy, but I didn't have time to work out the dimensions for each variable.

I've tested this with about 30 emails successfully unarchived in one go.  However if you try to unarchive many many emails you do so at your own risk.  Also if your colleagues are doing so at the same time, your IT department might not be overly happy.

The code will only move items which are of IPM.Note type - which means it will move archived and unarchived emails but not calendar appointments etc.

1 REPLY 1

Shane247
Not applicable

The above code doesn't work for me in Outlook 2010.  The messages get copied but they're still just a stub.  Any idea why not or what I can try?