Saving Send emails to a folder on the server

JohanKotze

Senior Member
OS Version(s)
  1. Windows
Outlook version
Outlook 2019 64-bit
Email Account
IMAP
Operating system::    Window 10 Pro
Outlook version:     Outlook 2019
Email type or host:    Email IMAP

Good day
I got this code from Slipstick Systems "Pick a Folder to save a send message". What I want to achieve is that when send button is clicked a dialog box on needs to open and the user will select between CommercialClients & PersonalClient then scroll to that client and save the message under sub dir Email Send

I am struggling to get this to work

Code:
Public Sub SaveMessageAsMsg()
  Dim oMail As Outlook.MailItem
  Dim objItem 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

StrUserPath = "\\JK_Server-PC\Users\JK_Server\My Documents\JKBrokers\Clients\"
StrFolderpath = BrowseForFolder(StrUserPath)
  
   For Each objItem In ActiveExplorer.Selection
   If objItem.MessageClass = "IPM.Note" Then
    Set oMail = objItem
    
  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
  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, ":", 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
 
Invalid:
 BrowseForFolder = False
End Function

When I run Debug to cursors the below dialog popup. When I run Debug to cursor the 2 dialog popup. I need the 1st dialog to popup. And there need be a nothing return if the user do not want to save the email.

Dialog.JPG 2 dialog.JPG
 
Not much interaction on this forum I persume. As my thread is related to Diane's code she posted on Slipstick.
 
Marco only runs from within VBA Editor and not from send button in Outlook. Private Sub ReplaceCharsForFileName(sName As String, _<br> sChr As String _<br>) gives an error.

I have a digital signed macro
 
Marco only runs from within VBA Editor and not from send button in Outlook. Private Sub ReplaceCharsForFileName(sName As String, _<br> sChr As String _<br>) gives an error.

I have a digital signed macro
That sub replaces unsupported filename characters in the subject.

What is the exact error message?
 
In a quickie test, it is working for me to save to a network folder.

Noticed this is duplicated.
Code:
Invalid:
    BrowseForFolder = False
End Function
 
Invalid:
 BrowseForFolder = False
End Function


To cleanly exit if clicking cancel, add the If statement after the strfolderpath line.
Code:
StrFolderpath = BrowseForFolder(StrUserPath)
 
 If StrFolderpath = "False" Then
      Cancel = True
      Exit Sub
  End If


On the wrong dialog - is the path correct? If it isn't, you'll get this dialog. If it is correct, test it with a shorter path and see if it brings up the correct folders (ie, use just \\JK_Server-PC\Users\). No need to create the subfolders - you should be able to tell by looking if it is using the path.
1724092861993.png
 
Hi Diane thank you so much for replying. I will apply your suggestions and revert back tomorrow. Thx a lot.
 
Hi Diane
I amand the code as follow and do the StrFolderPath bit for bit and it works. I add the cancel code. The duplication in BrowserForFolder was a typo and was not duplicated in Outlook VBA Editor.. So the code now looks like this
Code:
Public Sub SaveMessageAsMsg()
  Dim oMail As Outlook.MailItem
  Dim objItem 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

StrUserPath = "\\JK_Server-PC\Users\JK_Server\Documents\JKBrokers\Clients\"
StrFolderpath = BrowseForFolder(StrUserPath)

If StrFolderpath = "False" Then
      Cancel = True
      Exit Sub
  End If
 
   For Each objItem In ActiveExplorer.Selection
   If objItem.MessageClass = "IPM.Note" Then
    Set oMail = objItem
   
  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
  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, ":", 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

The I run Debug on Public Sub SaveMessageAsMsg()
StrUserPath = "\\JK_Server-PC\Users\JK_Server\Documents\JKBrokers\Clients\" popup display so far so good
1724139276178.png


Debug on
Private Sub ReplaceCharsForFileName(sName As String, _
sChr As String _
)
Brought nothing up nor any error , maybe because there is nothing

Debug on Function BrowseForFolder(Optional OpenAt As Variant) As Variant and the dialog box displayed
1724139230183.png

So I remove and add the digital signature again exit VBA Developer and outlook and restart Outlook. Dis test send email but no dialog box popup. So I went to VBA Editor and run the macro from there all is working good. Macro setting set to "Notifications for digitally signed macros, all other macro disabled I event set the Marco to low no popup

When I run the Marco from within VBA Editor the correct popup pops up
1724139966156.png
 
Debug on Function BrowseForFolder(Optional OpenAt As Variant) As Variant and the dialog box displayed
Running/debugging that will default to the default path - because you did not pass a location to it - that is the "Optional OpenAt As Variant" part.

The same would apply to ReplaceCharsForFileName - it requires a value (the string you want to clean) to work.
 
Running/debugging that will default to the default path - because you did not pass a location to it - that is the "Optional OpenAt As Variant" part.

The same would apply to ReplaceCharsForFileName - it requires a value (the string you want to clean) to work.
Hi Diane I understand the above argument

What I do not understand is the fact the (macro) popup is working in VBA Editor but not in Outlook when I click on Send. What do I do wrong
 
The macro - SaveMessageAsMsg - is not an automatic macro. It will run from a button you click on the ribbon - and applies to the selected messages.

You need to use the macro here if you want it automated.

It needs to be in ThisOutlookSession - the macro above needs tweaked to be added to the itemadd macro - specially, removing all of the code that deals with processing selected messages.


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 Item As Object)

-- snip --



Or use itemsend -
Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

 ' code to save goes here 

End Sub
 
The macro - SaveMessageAsMsg - is not an automatic macro. It will run from a button you click on the ribbon - and applies to the selected messages.

You need to use the macro here if you want it automated.

It needs to be in ThisOutlookSession - the macro above needs tweaked to be added to the itemadd macro - specially, removing all of the code that deals with processing selected messages.


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 Item As Object)

-- snip --



Or use itemsend -
Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

 ' code to save goes here

End Sub
Hi Diane like this if use the 2nd option
Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
  Dim oMail As Outlook.MailItem
  Dim objItem 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

StrUserPath = "\\JK_Server-PC\Users\JK_Server\Documents\JKBrokers\Clients\"
StrFolderpath = BrowseForFolder(StrUserPath)

If StrFolderpath = "False" Then
      Cancel = True
      Exit Sub
  End If
 
   For Each objItem In ActiveExplorer.Selection
   If objItem.MessageClass = "IPM.Note" Then
    Set oMail = objItem
    
  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
  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, ":", 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
 
Hi Diane like this if use the 2nd option
Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
  Dim oMail As Outlook.MailItem
  Dim objItem 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

StrUserPath = "\\JK_Server-PC\Users\JK_Server\Documents\JKBrokers\Clients\"
StrFolderpath = BrowseForFolder(StrUserPath)

If StrFolderpath = "False" Then
      Cancel = True
      Exit Sub
  End If
 
   For Each objItem In ActiveExplorer.Selection
   If objItem.MessageClass = "IPM.Note" Then
    Set oMail = objItem
   
  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
  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, ":", 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
HI Diane thanks for this. The is a hick up however. The dialog do popup and I save the email under the client's dir. But my message to the my client email before sending is not save only my client' email.
 
HI Diane thanks for this. The is a hick up however. The dialog do popup and I save the email under the client's dir. But my message to the my client email before sending is not save only my client' email.
What I just pick up is that I was sending another to another client and save it to the client's dir but the email that was accually save was the one in the above reply that was sent previously and not the current one.
 
What I just pick up is that I was sending another to another client and save it to the client's dir but the email that was accually save was the one in the above reply that was sent previously and not the current one.
Its as if the code hold the previous email and not delete or release it
 
It needs tweaked a little - removing the lines that deal with the selection and removing the DIMs and changing the object name in the "title" - the last is because its easier to change their than editing all the uses in the code.

I also moved the line "If oMail.MessageClass = "IPM.Note" Then" to before you choose the path - it makes more sense here. (In the manual macro you can choose a selection, which could include non-email items so it makes sense to check it during the saving part, after the path is set.)

Code:
Private Sub Application_ItemSend(ByVal oMail As Object, Cancel As Boolean)
  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
 
BTW - these are the lines that need to be removed - the first one applies to the selection - probably the previously sent message. The only thing left was to make sure we use oMail everywhere, not objitem.

For Each objItem In ActiveExplorer.Selection
If objItem.MessageClass = "IPM.Note" Then
Set oMail = objItem

and then the Next at the end.
 
BTW - these are the lines that need to be removed - the first one applies to the selection - probably the previously sent message. The only thing left was to make sure we use oMail everywhere, not objitem.

For Each objItem In ActiveExplorer.Selection
If objItem.MessageClass = "IPM.Note" Then
Set oMail = objItem

and then the Next at the end.
Thx Diane, you are really a star. Where were you all my life
 
Hi Diane just picking this up. The email I send is saved before sending

Save before sending.JPG
 
Similar threads
Thread starter Title Forum Replies Date
N Saving .msg as sent item on send Outlook VBA and Custom Forms 1
Rupert Dragwater Background colors not saving in Outlook 365 Using Outlook 15
R Saving Emails and Attachments as .msg file Using Outlook 3
CWM330 Saving Data: Don't check certain folders Using Outlook 2
M Saving emails using Visual Basic - Selecting folder with msoFileDialogFolderPicker Outlook VBA and Custom Forms 6
D Outlook 2016 Outlook not saving Sent Items Using Outlook 4
I Error saving screenshots in a custom form in outlook 2016, outlook 365 - ok in outlook 2013, outlook 2010 Outlook VBA and Custom Forms 5
I Saving attachments from multiple emails and updating file name Outlook VBA and Custom Forms 0
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
L Attachment saving and tracking - PLEASE help! Outlook VBA and Custom Forms 5
D Saving Selected Emails as PDF and saving Attachments Outlook VBA and Custom Forms 6
B Saving items under a folder Using Outlook 3
R Quick Access view in File Explorer when saving attachments Using Outlook 0
N Saving And Deleting Outlook Attachments with Unknown Error Message Outlook VBA and Custom Forms 1
V Saving attachment from outlook in My Documents Outlook VBA and Custom Forms 14
M Dialog called up multiple times when saving emails from macro Outlook VBA and Custom Forms 2
A saving attachement to folder named the same as rule name Outlook VBA and Custom Forms 0
T Saving all email to file folder in Windows Using Outlook 2
J Saving attachments from specific sender (phone number) to specific folder on hard drive Using Outlook 3
C Saving Outlook attachments and links to attachments with VBA Outlook VBA and Custom Forms 2
Kevin H Remotely saving emails Using Outlook 1
R Outlook 2010 Modify Style "Do not check spelling or grammar" not saving Outlook VBA and Custom Forms 0
R Outlook Office 365 not saving addresses Using Outlook 0
A Keep color categories when saving vCards Using Outlook 1
P Saving All Messages to the Hard Drive Using VBA Outlook VBA and Custom Forms 5
e_a_g_l_e_p_i question about saving my .pst so I can import it to my Outlook after I build a new system Using Outlook 10
S Editing an email with notes and saving it for record using Macro Outlook VBA and Custom Forms 3
O Saving Attachments to folder on disk and adding Initials to end of file name Outlook VBA and Custom Forms 9
J Outlook 2013 crashes saving VBA & clicking tools | digital signature Outlook VBA and Custom Forms 1
bifjamod Saving sent email to specific folder based on category with wildcard Outlook VBA and Custom Forms 1
erichamion Changes to meeting body not properly saving Outlook VBA and Custom Forms 4
A ItemAdd on Imap Folder get endless loop after saving item Using Outlook 5
T Saving Outlook 2010 email with attachments but read the email without Outlook Using Outlook 2
T From Field Blank when saving to folder other than Sent items Using Outlook 2
L Outlook DST (Daylight Saving Time) problem Using Outlook 0
F Using Outlook 2007 as an IMAP Mail Station Without Saving Data Locally Using Outlook 2
E Saving Changes To Edited E-Mail Received Message Using Outlook 0
D File Lock issue when saving message from Outlook to new folder Using Outlook 1
D Remove extension while saving attachments Using Outlook 1
K Printing & Saving Outlook Contacts Using Outlook 3
S Not saving attachments in the Sent Folder Using Outlook 2
S trouble with Outlook 2010 saving sent emails Using Outlook 2
D Saving outlook emails in html and attachments Using Outlook 4
W Default Saving a message as text Using Outlook 2
R Outlook 2007 QAT buttons not saving Using Outlook 2
C Exchange 2003 - Outlook 2003 - Calendar entries saving over each other Using Outlook 2
J Saving Published Outlook Form as msg Using Outlook 1
J Saving recent colors used for fonts in an email? Using Outlook 1
B How to choose which contacts folder to use when saving contacts? Using Outlook 1
J Saving Incoming & Outgoing Outlook 2010 Email Locally with IMAP Using Outlook 2

Similar threads

Back
Top