none
Move sent mails to other mailbox RRS feed

  • Question

  • I have a vba code that does the following.

    I create a new mail , type some tekst and click on SEND. Then a window pops up and I can choose in what folder this mail we be saved in. I can choose the SENT ITEMS (default) folder, or another one. This works ok.

    But when I choose a custom folder from another mailbox to which I have full access, it does not work (I have Outlook 2013). Because when I do this, the mail is just saved in my SENT ITEMS folder.

    Is this not possible or what do I need to do ?

    It should work in Outlook 2010 and 2013.

    Option Explicit
     
    Public Function SentFolder(ByRef Item As Object) As Boolean
     
        Dim objFolder As Object
        If Not Item.Class = olMail Then Exit Function
        Do
            Set objFolder = Nothing
            Set objFolder = Outlook.Session.PickFolder
            If objFolder Is Nothing Then
                SentFolder = True
                Exit Function
            End If
            If InStr(objFolder.DefaultMessageClass, "IPM.Note") = 0 Then
                Set objFolder = Nothing
                If MsgBox("Please choose folder" _
                    , vbCritical + vbOKCancel, "Ablage auswählen") = vbCancel Then
                    SentFolder = True
                    Exit Function
                End If
            End If
            If Not objFolder Is Nothing Then
                If objFolder = Outlook.Session.GetDefaultFolder(olFolderInbox) Then
                    If MsgBox("BLABLABLA" _
                        , vbExclamation + vbYesNo + vbDefaultButton2, "Ablage auswählen") = vbNo Then
                        Set objFolder = Nothing
                    End If
                End If
            End If
     
        Loop While objFolder Is Nothing
        Set Item.SaveSentMessageFolder = objFolder
        Set objFolder = Nothing
     
    End Function
    
    
    
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
        Cancel = SentFolder(Item)
        Set Item = Nothing
     
    End Sub

    Tuesday, September 9, 2014 7:09 AM

Answers

  • SaveSentMessageFolder property can only be set to the folder from the store where the message is residing.

    If you want a folder from a different mailbox, you would need to set the SaveSentMessageFolder property to a folder in the same mailbox (it can be your custom folder, not the Sent Items folder), process the MAPIFolder.Items.ItemAdd event on that folder, then explicitly move it to a different mailbox using MailItem.Move.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, September 9, 2014 7:16 AM
  • Since you allow the message to be moved to an arbitrary folder, you can store that folder's entry id (MAPIFolder.EntryID) and store entry id (MAPIFolder.StoreID) as user properties (MailItem.UserProperties.Add).

    At a later point (timer? Items.ItemAdd event firing on the temp folder, etc.) you can loop through all items in that temp folder, for each item read the folder and store entry ids stored as user properties, use them to call Application.Session.GetFolderFromID. And finally, you can call MailItem.Move.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, September 11, 2014 2:49 PM

All replies

  • SaveSentMessageFolder property can only be set to the folder from the store where the message is residing.

    If you want a folder from a different mailbox, you would need to set the SaveSentMessageFolder property to a folder in the same mailbox (it can be your custom folder, not the Sent Items folder), process the MAPIFolder.Items.ItemAdd event on that folder, then explicitly move it to a different mailbox using MailItem.Move.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, September 9, 2014 7:16 AM
  • OK, is there some example I can use. I am not a real developer , I just copied the code and used that one.

    Tuesday, September 9, 2014 7:17 AM
  • Hello Toon,

    See the Concepts (Outlook 2013 developer reference) section in MSDN. It provides a lot of sample code for various tasks.

    Also I'd recommend starting from the Getting Started with VBA in Outlook 2010 article which explains the basics of Outlook programming.

    Tuesday, September 9, 2014 12:02 PM
  • Got it :

    Loop While objFolder Is Nothing
        Item.UnRead = False
        Item.Move objFolder
      
        Set objFolder = Nothing

    Tuesday, September 9, 2014 12:33 PM
  • I don't think that code will do anything - objFolder is not null.

    Where would you invoke that code?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, September 9, 2014 1:14 PM
  • Most probably not all code are listed there. At least if all works as expected.
    Tuesday, September 9, 2014 1:24 PM
  • I'm sorry , it works only when I do a new mail or when the Reply or Forward is in a separate window.

    In Outlook 2013 when you do a Reply it is within Outlook, there you can choose 'Pop Out' and it becomes a separate window and then it works. But if I do not do that 'Pop out' thing ,

    I get the error : 'This method can't be used with an inline response mail item' on Item.Move objfolder

    HELP :)

    Wednesday, September 10, 2014 7:37 AM
  • The Explorer class provides the InlineResponse event which is fired when the user performs an action that causes an inline response to appear in the Reading Pane. So, you can handle the event to get an instance of the MailItem class which represents a new item. The ActiveInlineResponse property of the Explorer class returns an item object representing the active inline response item in the explorer reading pane.

    Wednesday, September 10, 2014 12:15 PM
  • Please show your latest code. Some methods (such as Send and Move) indeed cannot be called on the inline responses.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, September 10, 2014 1:22 PM
  • Public Function SentFolder(ByRef Item As Object) As Boolean
     
        Dim objFolder As Object
        If Not Item.Class = olMail Then Exit Function
        Do
            Set objFolder = Nothing
            Set objFolder = Outlook.Session.PickFolder
            If objFolder Is Nothing Then
                SentFolder = True
                Exit Function
            End If
            If InStr(objFolder.DefaultMessageClass, "IPM.Note") = 0 Then
                Set objFolder = Nothing
                If MsgBox("Please choose folder" _
                    , vbCritical + vbOKCancel, "Gelieve een andere folder dan INBOX te kiezen.") = vbCancel Then
                    SentFolder = True
                    Exit Function
                End If
            End If
            If Not objFolder Is Nothing Then
                If objFolder = Outlook.Session.GetDefaultFolder(olFolderInbox) Then
                    If MsgBox("Wilt de mails in INBOX bewaren ?" _
                        , vbExclamation + vbYesNo + vbDefaultButton2, "Andere folder") = vbNo Then
                        Set objFolder = Nothing
                    End If
                End If
            End If
     
        Loop While objFolder Is Nothing
        Item.UnRead = False
        Item.Move objFolder
        
        Set objFolder = Nothing
     
    End Function
    
    
    
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
        Cancel = SentFolder(Item)
        Set Item = Nothing
     
    End Sub



    • Edited by Toon Sillen Wednesday, September 10, 2014 1:36 PM
    Wednesday, September 10, 2014 1:35 PM
  • You cannot call Move on a MailItem passed to the ItemSend event, be that inline response or not. Again, as I mentioned before, you can set SaveSentMailFolder to a folder from the primary store, and process the item later after the message is actually sent and moved.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, September 10, 2014 1:43 PM
  • Dmitry,

    and how do I do that : "you can set SaveSentMailFolder to a folder from the primary store, and process the item later after the message is actually sent and moved."

    Thursday, September 11, 2014 1:28 PM
  • Create a folder in the primary store and name it any way you want.

    Set the MailItem.SaveSentMailFolder property to that folder.

    After the message is sent, process that folder (it will only contain your messages that you want to move) and move them explicitly to some other folder in a different store.

    const FolderName = "My Temp Sent Items Folder"
    on error resume next
    set RootFolder = Application.Session.GetDEfaultFolder(olFolderInbox).Parent
    set TenpFolder = RootFolder.Folders.Item(FolderName)
    If Err.Number <> 0 Then set TenpFolder = RootFolder.Folders.Add(FolderName)
    Err.Clear


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, September 11, 2014 1:37 PM
  • Ok so now I have this code, but then it does not move the mail to the folder of the other mailbox.

    
    Option Explicit
    Const FolderName = "mover"
     
    Public Function SentFolder(ByRef Item As Object) As Boolean
     
        Dim objFolder As Object
        Dim oRoot As Outlook.Folder
        Dim RootFolder As Outlook.Folder
        Dim TempFolder As Outlook.Folder
        If Not Item.Class = olMail Then Exit Function
        Do
            Set objFolder = Nothing
            Set objFolder = Outlook.Session.PickFolder
            If objFolder Is Nothing Then
                SentFolder = True
                Exit Function
            End If
            If InStr(objFolder.DefaultMessageClass, "IPM.Note") = 0 Then
                Set objFolder = Nothing
                If MsgBox("Please choose folder" _
                    , vbCritical + vbOKCancel, "Gelieve een andere folder dan INBOX te kiezen.") = vbCancel Then
                    SentFolder = True
                    Exit Function
                End If
            End If
            If Not objFolder Is Nothing Then
                If objFolder = Outlook.Session.GetDefaultFolder(olFolderInbox) Then
                    If MsgBox("Wilt de mails in INBOX bewaren ?" _
                        , vbExclamation + vbYesNo + vbDefaultButton2, "Andere folder") = vbNo Then
                        Set objFolder = Nothing
                    End If
                End If
            End If
     
        Loop While objFolder Is Nothing
        Item.UnRead = False
        'Item.Move objFolder
        
        
        Set Item.SaveSentMessageFolder = objFolder
    
        
    On Error Resume Next
    Set RootFolder = Application.Session.GetDefaultFolder(olFolderInbox).Parent
    Set TempFolder = RootFolder.Folders.Item(FolderName)
    If Err.Number <> 0 Then Set TempFolder = RootFolder.Folders.Add(FolderName)
    Err.Clear
    
        
        Set objFolder = Nothing
     
    End Function
    
    
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
        Cancel = SentFolder(Item)
        Set Item = Nothing
     
    End Sub
    

    Thursday, September 11, 2014 2:04 PM
  • You never use TempFolder, why do you expect anything to happen? Have you tried to use it to set the SaveSentMailFolder  property? You also need to get rid of your "while" loop.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, September 11, 2014 2:14 PM
  • I'm sorry, I have no idea what I'm doing;

    I tried this but that just saves it in the tempfolder (mover)

       
    On Error Resume Next
    Set RootFolder = Application.Session.GetDefaultFolder(olFolderInbox).Parent
    Set TempFolder = RootFolder.Folders.Item(FolderName)
    If Err.Number <> 0 Then Set TempFolder = RootFolder.Folders.Add(FolderName)
    Err.Clear

    Set Item.SaveSentMessageFolder = TempFolder 'objFolder

        Item.Move TempFolder
        Set objFolder = Nothing

    Thursday, September 11, 2014 2:21 PM
  • You have what you want. Next step would be to process the items in that folder at a later moment when the message is actually sent and moved there.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, September 11, 2014 2:24 PM
  • ok, and how do I move it to the folder I selected in my popup window ?
    Thursday, September 11, 2014 2:26 PM
  • Since you allow the message to be moved to an arbitrary folder, you can store that folder's entry id (MAPIFolder.EntryID) and store entry id (MAPIFolder.StoreID) as user properties (MailItem.UserProperties.Add).

    At a later point (timer? Items.ItemAdd event firing on the temp folder, etc.) you can loop through all items in that temp folder, for each item read the folder and store entry ids stored as user properties, use them to call Application.Session.GetFolderFromID. And finally, you can call MailItem.Move.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, September 11, 2014 2:49 PM