Diane Poremsky
Senior Member
- OS Version(s)
- MacOS
- Windows
- iOS
- Android
- Outlook version
- Outlook 2016 32 bit
- Email Account
- Office 365 Exchange
It's working here - but it is saved as an unsent message. If you want to save it after it was sent, you need to use the itemadd macro - after adding it to thisoutlooksession, click in application startup and click run to kick start it.
Code:
Private WithEvents objSentItems As Items
Private Sub Application_Startup()
Dim objSent As Outlook.MAPIFolder
Set objNS = Application.GetNamespace("MAPI")
Set objSentItems = objNS.GetDefaultFolder(olFolderSentMail).Items
Set objNS = Nothing
End Sub
Private Sub objSentItems_ItemAdd(ByVal oMail As Object)
Dim sPath As String
Dim dtDate As Date
Dim sName As String
Dim StrFolderpath As String
Dim StrUserPath As Variant
'Defaults to Documents folder
'get the function at http://slipstick.me/u1a2d
If oMail.MessageClass = "IPM.Note" Then
StrUserPath = "C:\Users\JennyWren\OneDrive - Slipstick Systems\Documents\"
StrFolderpath = BrowseForFolder(StrUserPath)
If StrFolderpath = "False" Then
Cancel = True
Exit Sub
End If
sName = oMail.Subject
ReplaceCharsForFileName sName, "-"
dtDate = oMail.ReceivedTime
sName = Format(dtDate, "ddmmyyyy", vbUseSystemDayOfWeek, _
vbUseSystem) & Format(dtDate, "-hhnnss", _
vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg"
sPath = StrFolderpath & "\"
Debug.Print sPath & sName
oMail.SaveAs sPath & sName, olMSG
End If
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, ":", 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
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
Dim ShellApp As Object
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
Set ShellApp = Nothing
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
BrowseForFolder = False
End Function