VBA code to save multiple emails as text files
top of page

VBA code to save multiple emails as text files


You can use the below visual basic code posted here on Slip Stick, to to convert multiple selected emails to separate text files. (Follow the link to 'SaveSelectedMailAsTxtFile.)

Enter your own file path in the code on the line beginning, oMail.SaveAs, for the folder in which you want the text files to be generated.

If you select multiple emails in an Outlook folder, and the run the macro . . .

. . . text files will be generated beginning the date and time of the emails.

Sub SaveSelectedMailAsTxtFile() Const OLTXT = 0 Dim currentExplorer As Explorer Dim Selection As Selection Dim oMail As Outlook.MailItem Dim obj As Object Dim sPath As String Dim dtDate As Date Dim sName As String

Set currentExplorer = Application.ActiveExplorer Set Selection = currentExplorer.Selection

For Each obj In Selection Set oMail = obj sName = oMail.Subject ReplaceCharsForFileName sName, "_" dtDate = oMail.ReceivedTime sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _ vbUseSystem) & Format(dtDate, "-hhnnss", _ vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".txt" oMail.SaveAs "C:\Users\Diane\Dropbox\For EMO\" & sName, OLTXT Next End Sub Private Sub ReplaceCharsForFileName(sName As String, _ sChr As String _ ) sName = Replace(sName, "/", sChr) sName = Replace(sName, "\", sChr) sName = Replace(sName, ":", sChr) sName = Replace(sName, "?", sChr) sName = Replace(sName, Chr(34), sChr) sName = Replace(sName, "<", sChr) sName = Replace(sName, ">", sChr) sName = Replace(sName, "|", sChr) End Sub


bottom of page