Looking for a code snippet to export a selected folder to PST.
I have literally hundreds of email folders for each project I work on. My account is almost at the O365 50GB limit and to I need to start archiving these folders. I can't seem to find any API functions to export to PST.
This should do what you need. Note that the code will move all the items in the folder to the new PST file, so you should do a few practice runs with a folder of dummy emails, etc. to make sure it works first. Also, you will need to make adjustments to the file path and the filename, etc. As it is, it currently sources part of the filename from the folder name.
Rich (BB code):
Dim olNS As outlook.NameSpace
Dim olBackup As outlook.folder
Dim olSourceFolder As outlook.folder
Dim olSourceItem As Object
Dim strPath As String
Dim strDisplayName As String
Set olNS = GetNamespace("MAPI")
Set olSourceFolder = olNS.PickFolder
strDisplayName = olSourceFolder.name & "-Archive_" & Format(date, "yyyymmdd-hhmmss")
strPath = "C:\TEMP\" & strDisplayName & ".pst"
'Creates a PST file at the path (strPath).
Set olArchive = olNS.Folders.GetLast
olArchive.name = strDisplayName
'Moves across all the items in the selected folder to the PST
For counter = olSourceFolder.Items.Count To 1 Step -1
Set olSourceItem = olSourceFolder.Items(counter)
'Closes and removes the PST file
Set olNS = Nothing
Set olSourceFolder = Nothing
Set olArchive = Nothing
Set olSourceItem = Nothing
Export a folder to pst file
Click File > Open & Export > Import/Export in Outlook 2013 and 2016. ...
In the first step of Import and Export Wizard, select Export to a file, and then click Next.
Then select Outlook Data File
Select the specified folder that you want to export as pst file in the Export Outlook Data File dialog, and click Next.