[Solved] Save outlook mail automatically to a specified folder [closed]


Set Macro security low in your Outlook:
go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.

Open the VBA Editor:

To put the code in a module:

  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the below macro into the new module.

        Public Sub SaveMsg(Item As Outlook.MailItem)
    Dim sPath As String
    Dim dtDate As Date
    Dim sName As String
    Dim enviro As String 
    enviro = CStr(Environ("USERPROFILE"))
    sName = Item.Subject
    ReplaceCharsForFileName sName, "_" 
    dtDate = Item.ReceivedTime
    sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
    vbUseSystem) & Format(dtDate, "-hhnnss", _
    vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg"
    ' use My Documents in older Windows.     
    sPath = enviro & "\Documents\"
    Debug.Print sPath & sName
    Item.SaveAs sPath & sName, olMsg
    End Sub
    Private Sub ReplaceCharsForFileName(sName As String, _
    sChr As String _
    )
    sName = Replace(sName, "https://stackoverflow.com/", 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
    

This website gave me the perfect answer:

https://www.slipstick.com/outlook/archive-outlook/save-incoming-messages-hard-drive/

Just had to copy paste their code and change the basic parameters.
Works like gun.

0

solved Save outlook mail automatically to a specified folder [closed]