I recently copied all my e-mails out of an archive folder, so I could zip them up on my hard drive. To do this I wanted to rename each file with the date the email was sent, using the format YYYY-MM-DD.
The VBA code below works for me 🙂
As always 1, please ensure you test any script taken from my website on a test/development machine, before running on a production server.
Sub GetMessageDate() Dim OlApp As Outlook.Application Set OlApp = GetObject(, "Outlook.Application") Dim MsgFilePath Dim Eml As Outlook.MailItem Dim Path As String Path = "C:\Outlook Files\" If OlApp Is Nothing Then Err.Raise ERR_OUTLOOK_NOT_OPEN End If Dim fs As Object Set fs = CreateObject("Scripting.FileSystemObject") Dim temp As Object Set temp = fs.GetFolder(Path) For Each MsgFilePath In temp.files Set Eml = OlApp.CreateItemFromTemplate(Path & MsgFilePath.Name) 'Now rename the file Name Path & MsgFilePath.Name As Path & Format(Eml.SentOn, "YYYY-MM-DD") & " " & MsgFilePath.Name Set Eml = Nothing Next Set OlApp = Nothing End Sub
- Please ensure you test any script taken from my website on a test/development machine (using copies of your msg files), before running on a production server. ↩