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?
LVL 4
Tony PittAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", 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".
0
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 ...

/T
0
Qlemo"Batchelor", 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.
0
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.

/T
0
Qlemo"Batchelor", 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
  Next
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))
  Next
  
  Set CreateFolder = dest
End Function

Open in new window


This will copy the mail from e.g.
   MyMail\Inbox\Sorted\Just not important
to
   Archive\Inbox\Sorted\Just not important
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.