VBA macro to file sent mails

Gaston_63 40 Reputation points
2025-07-31T07:01:58.9666667+00:00

I am using since many years a macro which executes when I send a mail and asks me, in which folder I want to store this sent mail (instead of leaving it in the "sent mail folder"). This macro is:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

If TypeOf Item Is Outlook.MailItem Then

SaveSentMail Item

End If

End Sub

Private Sub SaveSentMail(Item As Outlook.MailItem)

Dim F As Outlook.MAPIFolder

If Item.DeleteAfterSubmit = False Then

Set F = Application.Session.PickFolder

If Not F Is Nothing Then

Set Item.SaveSentMessageFolder = F

End If

End If

End Sub

The problem I have is that I have 2 mailboxes in Outlook, one is on a hosted Exchange server, and the other is a Gmail address. The macro however only kicks in when I send a mail from the Exchange mailbox and I would like it to also cover the Gmail address.

Thank you in advance for some advice!

Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

Accepted answer
  1. Hornblower409-4652 1,305 Reputation points
    2025-08-03T08:18:28.3633333+00:00

    I assume your original code came from

    Pick the Folder Manually at
    https://www.vboffice.net/en/developers/determine-folder-for-sent-items/

    In which case I think the reason it is not firing on a GMail send is because of the line:
    If Item.DeleteAfterSubmit = False Then
    Please test with this modified version and see if this works.

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
      If TypeOf Item Is Outlook.MailItem Then
        SaveSentMail Item
      End If
    End Sub
    
    Private Sub SaveSentMail(Item As Outlook.MailItem)
      Dim F As Outlook.Folder
        Item.DeleteAfterSubmit = False
        Set F = Application.Session.PickFolder
        If Not F Is Nothing Then
          Set Item.SaveSentMessageFolder = F
        End If
    End Sub
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Emmanuel Santana 23,115 Reputation points Independent Advisor
    2025-07-31T07:54:49.5033333+00:00

    Hello. Indeed, your original macro doesn’t work with Gmail because Outlook respects the SaveSentMessageFolder property only for Exchange accounts. Since your Gmail account is likely set up as IMAP, Outlook ignores this setting and always saves sent emails in the default "Sent Mail" folder.

    Try using this:

    Option Explicit
    ' Module-level variable to hold the folder chosen before send
    Private gTargetFolder As Outlook.MAPIFolder
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
        If TypeOf Item Is Outlook.MailItem Then
            Dim acc As Outlook.Account
            Set acc = Item.SendUsingAccount
            
            If acc Is Nothing Then Exit Sub
            
            Set gTargetFolder = Application.Session.PickFolder
            If gTargetFolder Is Nothing Then Exit Sub ' User cancelled folder pick
            
            If acc.AccountType = olExchange Then
                ' Exchange accounts: set folder, Outlook handles saving
                Set Item.SaveSentMessageFolder = gTargetFolder
                Set gTargetFolder = Nothing ' Clear after use
            Else
                ' Non-Exchange accounts: cannot set SaveSentMessageFolder
                ' Sent mail will be saved in default Sent Items
                ' We'll move it later in ItemSendComplete
                Set Item.SaveSentMessageFolder = Nothing
                ' gTargetFolder remains set for use in ItemSendComplete
            End If
        End If
    End Sub
    Private Sub Application_ItemSendComplete(ByVal Item As Object)
        On Error Resume Next
        
        If gTargetFolder Is Nothing Then Exit Sub ' No folder selected or Exchange case
        
        If TypeOf Item Is Outlook.MailItem Then
            Dim acc As Outlook.Account
            Set acc = Item.SendUsingAccount
            
            If acc Is Nothing Then Exit Sub
            
            ' Only process non-Exchange accounts
            If acc.AccountType <> olExchange Then
                Dim sentFolder As Outlook.MAPIFolder
                Set sentFolder = Application.Session.GetDefaultFolder(olFolderSentMail)
                
                ' Confirm the sent mail is currently in Sent Items (default)
                If Item.Parent.EntryID = sentFolder.EntryID Then
                    ' Move the sent mail to the user-chosen folder
                    Item.Move gTargetFolder
                End If
            End If
        End If
        
        Set gTargetFolder = Nothing ' Clear for next send
    End Sub
    
    
    

    This macro works basically the same, the only difference is that for Exchange, Outlook saves sent mail directly to your chosen folder; for Gmail, the macro must move it afterward because Outlook always saves to the default Sent folder (that's how it handles IMAP configuration).

    Let me know if this works for you or if you see any error.


  2. Gaston_63 40 Reputation points
    2025-07-31T12:48:31.1666667+00:00

    Thank you very much! It works on the Exchange account and on the Gmail account the window with the folder pops up, but then I get a run-time error (I am pasting the screenshot below).
    The error is on the code line Set Item.SaveSentMessageFolder = Nothing

    Any idea how that can be fixed?

    Thanks again!User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.