At times, you may want to batch export an Outlook folder with all subfolders and items to a Windows folder. This article will teach you such a method that is applying Outlook VBA.
When you would like to export an Outlook folder to local drive with all items in the same folder structure, if you select to save and export manually, it’ll take you a lot of time. Thus, why don’t you resort to other means, such as any export tools or VBA codes? Here we will unveil such a piece of VBA code to you. It will permit you to achieve it like a breeze.
Export All Subfolders & Items in an Outlook Folder to a Windows Folder
- At the very outset, start your Outlook program.
- Then in the main Outlook window, press the “Alt + F11” key shortcuts.
- Subsequently, the “Microsoft Visual Basic for Applications” window will pop up.
- Next you need to open a blank module and copy the following VBA codes into it.
Private objFileSystem As Object Private Sub ExportFolderWithAllItems() Dim objFolder As Outlook.Folder Dim strPath As String 'Specify the root local folder 'Change it as per your needs strPath = "E:\Outlook\" Set objFileSystem = CreateObject("Scripting.FileSystemObject") 'Select a Outlook PST file or Outlook folder Set objFolder = Outlook.Application.Session.PickFolder Call ProcessFolders(objFolder, strPath) MsgBox "Complete", vbExclamation End Sub Private Sub ProcessFolders(objCurrentFolder As Outlook.Folder, strCurrentPath As String) Dim objItem As Object Dim strSubject, strFileName, strFilePath As String Dim objSubfolder As Outlook.Folder 'Create the local folder based on the Outlook folder strCurrentPath = strCurrentPath & objCurrentFolder.Name objFileSystem.CreateFolder strCurrentPath For Each objItem In objCurrentFolder.Items strSubject = objItem.Subject 'Remove unsupported characters in the subject strSubject = Replace(strSubject, "/", " ") strSubject = Replace(strSubject, "\", " ") strSubject = Replace(strSubject, ":", "") strSubject = Replace(strSubject, "?", " ") strSubject = Replace(strSubject, Chr(34), " ") strFileName = strSubject & ".msg" i = 0 Do Until False strFilePath = strCurrentPath & "\" & strFileName 'Check if there exist a file in the same name If objFileSystem.FileExists(strFilePath) Then 'Add a sequence order to the file name i = i + 1 strFileName = strSubject & " (" & i & ").msg" Else Exit Do End If Loop 'Save as MSG file objItem.SaveAs strFilePath, olMSG Next 'Process subfolders recursively If objCurrentFolder.folders.Count > 0 Then For Each objSubfolder In objCurrentFolder.folders Call ProcessFolders(objSubfolder, strCurrentPath & "\") Next End If End Sub
- After that, you need to ensure that your Outlook permits macros in the macro settings.
- Eventually, you can have a try.
- Firstly, back to the new macro window.
- Next click into “ExportFolderWithAllItems” subroutine.
- Then press F5 key button to run this macro.
- After that, you need to select a specific folder.
- Lastly, when you get a message saying “Complete”, you can access the predefined local folder. You’ll find all items have been saved in the same folder structure.
Prevent Data Loss from Outlook Crashes
Perhaps you have ever encountered many Outlook crashes. Most of time, after a restart, Outlook will be able to work as normal. However, there is also a case that our PST file may get corrupted. At that time, you will try best to retrieve your PST data, such as recurring to an experienced tool like DataNumen Outlook Repair. It is able to fix Outlook errors and extract data from compromised PST file without breaking a sweat.
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including SQL Server repair and outlook repair software products. For more information visit www.datanumen.com