How to Batch Export All Emails in a Conversation as Text Files via Outlook VBA

Sometimes, you may wish to batch capture all the emails in a specific conversation and then batch export them to local folder as Text files. This article will teach you how to get it in quick time.

Batch Export All Emails in a Conversation as Text Files via Outlook VBAFor some reason, you may need to export all the emails in a certain conversation to your local drive. If you manually do this, namely finding and saving one by one, it will be a tedious and arduous task. Therefore, you had better seek other quicker method, such as the following one. It will use a piece of VBA code to get it without breaking a sweat.

Batch Export All Mails in a Conversation as Text Files

  1. At the very outset, launch your Outlook application.
  2. Then press “Alt + F11” key buttons to access Outlook VBA editor.
  3. Subsequently, in the new window, you need to open a module that isn’t in use or directly insert a new module.
  4. Next you should copy and paste the following VBA codes into this module.
Public strFilePath, strFileName As String

Sub ExportMailsInConversationAsTXT()
    Dim objSelectedMail As Outlook.MailItem
    Dim objConversation As Outlook.Conversation
    Dim objMail As Outlook.MailItem

    Set objSelectedMail = ActiveExplorer.Selection.Item(1)
    Set objConversation = objSelectedMail.GetConversation
 
    If Not (objConversation Is Nothing) Then
       'Get all root items in this conversation
       For Each objMail In objConversation.GetRootItems
 
          strFileName = objMail.Subject
 
          'Remove the unsupported characters in email subject
          strFileName = Replace(strFileName, "/", " ")
          strFileName = Replace(strFileName, "\", " ")
          strFileName = Replace(strFileName, ":", "")
          strFileName = Replace(strFileName, "?", " ")
          strFileName = Replace(strFileName, Chr(34), " ")

          strFileName = Format(objMail.ReceivedTime, "YYYY-MM-DD") & "_" & strFileName & ".txt"
 
         'Export as Text files
         'Change "E:\" to other local folder path as per your needs
         strFilePath = "E:\" & strFileName
         objMail.SaveAs strFilePath, OLTXT

         'Process all children as well
         Call ProcessChildren(objMail, objConversation)
      Next
   End If

   MsgBox "Complete!", vbExclamation
End Sub

Sub ProcessChildren(objCurMail As Outlook.MailItem, objCurConversation As Outlook.Conversation)
    Dim objItems As Outlook.SimpleItems
    Dim objItem As Outlook.MailItem
 
    Set objItems = objCurConversation.GetChildren(objCurMail)

    If objItems.Count > 0 Then
       For Each objItem In objItems
 
           strFileName = objItem.Subject
 
           strFileName = Replace(strFileName, "/", " ")
           strFileName = Replace(strFileName, "\", " ")
           strFileName = Replace(strFileName, ":", "")
           strFileName = Replace(strFileName, "?", " ")
           strFileName = Replace(strFileName, Chr(34), " ")
 
           strFileName = Format(objItem.ReceivedTime, "yyyy-mm-dd") & "_" & strFileName & ".txt"
 
           strFilePath = "E:\" & strFileName
           objItem.SaveAs strFilePath, OLTXT
 
           'Process all children recursively
           Call ProcessChildren(objItem, objCurConversation)
       Next
    End If
End Sub

VBA Code - Batch Export All Emails in a Conversation as Text Files

  1. After that, you can add the new VBA project to Quick Access Toolbar as usual.
  2. Later ensure your Outlook macro security level to low.
  3. Lastly, you could have a try.
  • Firstly, select an email.
  • Then click the new macro button in Quick Access Toolbar.
  • When you get the message “Complete”, you can open the predefined local folder, in which you will see the emails in “TXT” format.

Tips for Restoring PST Data after Corruption

Often, in Outlook, you may encounter various Outlook errors, some of which can be blazing serious so that they straightly lead to PST corruption. At that time, you need to take actions to repair PST file to find back your valuable data. In this case, you can just recur to a reputable and well-proven tool, 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 recover sql server and outlook repair software products. For more information visit www.datanumen.com

Leave a Reply

Your email address will not be published. Required fields are marked *