How to Merge PST Files via Outlook VBA

If you want to merge multiple PST files into a single file, you can use the method introduced in this article. It will let you to quickly get it via Outlook VBA.

Merge PST Files via Outlook VBAIf you have many PST files, you will surely find it quite difficult for you to manage all of them in the meantime. More specifically, if you are frequently required to access the data in these PST files, it will be a tedious task for you to go through all the files to find out what you desire. In this case, the best solution is to merge all of them into a single PST file, such that you will be able to manage them far more easily and effectively.

In general, if you only would like to merge two files into one, you can just import one into the other one. However, if you want to merge multiple ones, doing this manually will be considerably troublesome. So, here we’ll introduce you to merge multiple PST files in quick time via VBA. Now, read on to get it in detail.

Merge PST Files via Outlook VBA

  1. In the first place, you should open all the PST files which you want to merge. About batch opening multiple PST files, you can refer to our another article – “How to Batch Open Multiple Outlook PST Files at Once in Your Outlook
  2. After opening them, you should access Outlook VBA editor, either by clicking “Visual Basic” button under “Developer” tab or pressing “Alt + F11” keys.
  3. Then in the “Microsoft Visual Basic for Applications” window, open a module.
  4. Next copy and paste the following VBA codes into the module window.
Public objNewPSTFileFolder As Outlook.Folder

Private Sub CreateNewPSTFile()
    'Create a new PST file
    Outlook.Application.Session.AddStore "E:\NewPSTMerge3.pst"
    Set objNewPSTFileFolder = Session.Folders.GetLast()
    Call SelectANDMergePSTFiles
End Sub

Sub SelectANDMergePSTFiles()
    Dim objSourceFile As Object
    Dim strMsg As String
    Dim nResponse As Integer
 
    'Select the source PST file
    Set objSourceFile = Outlook.Application.Session.PickFolder
    Call CopyFolder(objSourceFile)
 
    'Ask if select one more PST file
    strMsg = "One Completes! Do you want to select one more PST file?"
    nResponse = MsgBox(strMsg, vbExclamation + vbYesNo, "Merge PST Files")
  
    If nResponse = vbYes Then
       Call SelectANDMergePSTFiles
    Else
       MsgBox ("All Complete!")
    End If
End Sub

Sub CopyFolder(ByVal objCurrentFile As Object)
    Dim objFolder As Outlook.Folder
 
    For Each objFolder In objCurrentFile.Folders
        objFolder.CopyTo objNewPSTFileFolder
    Next objFolder
End Sub

VBA Code - Merge PST Files via Outlook VBA

  1. Subsequently, you should confirm that your Outlook permits macros.
  2. Finally you can have a try.
  • Firstly, click into the “CreateNewPST” subroutine.
  • Then click the “Run” icon in the toolbar.
  • Next you will need to select the source file in the popup dialog box like the following screenshot:Select Source File
  • After selecting one, click “OK”.
  • Then Outlook will copy all the items from the selected file to the new PST file.
  • After that, you will get a message asking if to select one more file. Hit “Yes”, you can select the next source PST file.Message asking if to select one more file
  • As long as you finish selecting and merging all of the desired source files, you can select “No”.
  • Lastly, you will successfully get a new PST file which is the merged file of all of your selected source files.

What if PST File Gets Corrupted

As Outlook PST file is susceptible to error and damage, you have to make efforts to safeguard your PST data. With no doubt, one of the most effective precautions is to persist in regular data backups, which will allow you to recover Outlook data much more easily. Moreover, you are better off keeping a potent repair tool handy, like DataNumen Outlook Repair.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including fix SQL Server and outlook repair software products. For more information visit www.datanumen.com

Leave a Reply

Your email address will not be published.