Copy old emails (> 6months) from Outlook to external location with outlook add-in

Viewed 92

We have created a add-in for outlook to archive inactive emails to a harddrive location. It all works fine, except for e-mails that are not visible in the outlook folder because they are older than 6 months (or whatever setting they used).

Since it is not desirable to change the settings to show all e-mails on the server, I would rather try to set this in the plugin on folder level, or have another workaround to still copy e-mails that are not visible.

For i = 1 To NumberOfFolders
   FolderTitle = objFolder.Folders(i).Name.ToString
   FolderTitle = RemoveSpecialChars(FolderTitle)
   Dim FolderPath As String
   FolderPath = Link & FolderTitle & "\"

   If Not System.IO.Directory.Exists(FolderPath) Then
      MkDir(Link & FolderTitle)
   End If

   emailorder = 0

   For Each Item In objFolder.Folders(i).Items
      emailorder = emailorder + 1
      Title = Strings.Right("000" & emailorder, 4) & " - " & RemoveSpecialChars(Item.Subject)
      path = FolderPath & Title & ".msg"
      Item.SaveAs(path)
      ProgressValue = ProgressValue + 1
      ProgressBarArchiving.Value = ProgressValue
   Next
      objFolder.Folders(i).Delete()
      NumberOfFolders = objFolder.Folders.Count
      If NumberOfFolders > 0 Then
         i = 0
      Else
         MsgBox(ProgressValue & " e-mails archived")
         Me.Close()
         Exit Sub
      End If
   Next

So I am looking for a solution where I can find and copy the e-mails which are not visible in outlook for the user, or where I can make all e-mails visible on folder level to copy all of them without overloading the mailbox with a lot of old e-mails.

As a test I created the following (msgbox are just to see what happens:

Dim Session As RDOSession = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = app.Session.MAPIOBJECT

Dim store As RDOExchangeMailboxStore = Session.Stores.DefaultStore
Dim storeID As String = store.EntryID
MsgBox("StoreID: " & storeID)

Dim FolderTest As RDOFolder
FolderTest = Session.PickFolder()
Dim FolderTestID As String = FolderTest.EntryID
MsgBox("FolderTestID: " & FolderTestID)

Dim MAPI_NO_CACHE As Integer
'Dim MAPI_BEST_ACCESS As Integer

Dim FolderTest2 As RDOFolder = Session.GetFolderFromID(FolderTest.EntryID,, MAPI_NO_CACHE)
Dim NumItems As Long = FolderTest2.Items.Count
MsgBox("NumItems: " & NumItems)

For Each Item In FolderTest2.Items
   MsgBox(Item.Subject)
Next
1 Answers

On the Extended MAPI level (C++ or Delphi only), you would need to open the folder in the online mode bypassing the cached store - you can do that by using the MAPI_NO_CACHE flag when calling IMsgStore::OpenEntry. When you then call IMAPIFolder::GetContentsTable, you will get all items in the folder from the remote server.

If C++ or Delphi are not an option, you can use Redemption (I am its author - any language) - you can create an instance of the RDOSession object, set its RDOSession.MAPIOBJECT property to Application.Session.MAPIOBJECT from OOM to make sure the two share the same MAPI session, then call RDOSesssion/RDOStore.GetFolderFromID specifying the folder entry id as well as MAPI_NO_CACHE (0x200) or'ed with MAPI_BEST_ACCESS (0x10) flags to open the folder in the online mode. An extra advantage is that you can run that code in a secondary thread (OOM won't let you do that).

You can also use Redemption to figure out whether the folder actually needs to be opened in the online mode - RDOExchangeMailboxStore.DaysToKeepOffline / MonthsToKeepOffline. If MonthsToKeepOffline == 0, all items are cached and can be accessed from the local cached store without opening the parent folder in the online mode.

Related