Automating creation of folders in Outlook pst file

I use a rich set of folders in Outlook 2013.  At the end of the calendar year, I want to move most (but not all) email into a similar set of folders in an archive pst file for the year.  I feel it ought to be possible to automate the creation of the set of folders in the archive pst folder to match those in the main data file but I don't know how to go about this.

Can anyone help me, please?
Tony PittAsked:
Who is Participating?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Sorry it took so long. Here is code which will move all selected mails to another PST file, maintaining the folder tree:
' Moves selected mails to a hard-coded archive PST, replicating the folder tree
Sub MoveToArchive()
Dim ml As MailItem
Dim itm As Object
Dim PST As Folder, dest As Folder
  Set PST = Session.Folders("Archive")
  For Each itm In ActiveExplorer.Selection
    If itm.Class = olMail Then
      Set ml = itm
      Set dest = CreateFolder(ml.Parent, PST)
      ml.Copy.Move dest  ' for testing. Remove the .Copy if sure.
    End If
End Sub

Private Function CreateFolder(fld As Folder, dest As Folder) As Folder
Dim flds, i
  flds = Split(Mid(fld.FolderPath, 3), "\")
  For i = LBound(flds) + 1 To UBound(flds) ' we ignore first = root folder
    On Error Resume Next
    dest.Folders.Add flds(i)
    On Error GoTo 0
    Set dest = dest.Folders(flds(i))
  Set CreateFolder = dest
End Function

Open in new window

This will copy the mail from e.g.
   MyMail\Inbox\Sorted\Just not important
   Archive\Inbox\Sorted\Just not important
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If you use the archiving process, it should do exactly that.
Any other approach requires VBA code duplicating the folder tree "manually".
Tony PittAuthor Commented:
Thanks, Olemo.  I was aware that archiving could *almost* do what I want, but it applies at a folder level, whereas I want to move the email manually so that I can select on a per-email basis.  Therefore, I think, I'm after VBA code to duplicate the folder tree ...

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Depending on the ratio of emails to move vs. to keep, how about code moving the selected mails to a fixed-name archive PST?
Just duplicating the folder tree doesn't save you from having to navigate thru both trees when moving. With above you can select all mails of the current folder, unselect a few, and then have the code move'm.
Tony PittAuthor Commented:
I'm sorry, I have used the archive mechanism.  It is not what I need.  I am happy to do the selection and moving of emails by hand.  All I need is the folders created, because that is extremely tedious to do manually.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.