Auto save outlook attachments when email is received

cmorten82

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Okay So I keep trying to run the following code with no luck. I just want to auto save the files to the file path mentioned below when the subject line is: Report: Advancement Transaction Detail

and I am not savvy in VBA so please dumb it down for me.

Code:
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, "Report: Advancement Transaction Detail"
  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, "/", 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
My File path is:

R:\Departments\Budget Services\Authorized Access Only\FY18 Budget\DB Management\Transactions Detail
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
so you have sPath = "R:\Departments\Budget Services\Authorized Access Only\FY18 Budget\DB Management\Transactions Detail" in place of sPath = enviro & "\Documents\" ? (You need a trailing \ on the file path, otherwise it is saved to DB Management folder, with Transactions Detail as part of the file name. )

Does it work if you use sPath = enviro & "\Documents\" to save it in your documents folder? (Outlook can be fussy about saving to a network location. It usually works if you have the drives mapped though.)
 
Top