none
Need an Outlook macro that moves emails by senders name from a specific folder to another folder RRS feed

  • Question

  • Hello,

    I need a macro that moves emails from a specific sender from a specific folder to another folder. Let me try to illustrate:

    1. I have multiple email accounts in my outlook.
    2. I send emails from these different accounts (Example: username@domain.com, service@domain.com, company@domain.com)
    3. When I send an email from "username@domain.com" a copy is copied into the "Sent Items" folder, as it should.
    4. However when I send it from "service@domain.com" it saves a copy in the "Sent Items" folder for service@domain.com which is what I WANT it to do, but it also makes a copy in username@domain.com "Sent Items" which is what I DON'T want it to do.
    5. What I want is every time the copy from the email "service@domain.com" is put into "username@domain.com"s "Sent Items" I want it moved into a folder called "SERVICE".
    Wednesday, July 15, 2015 7:03 AM

Answers

  • Hello,

    You may handle the ItemAdd event of the Items class (comes from the folder where emails are located) which is fired when one or more items are added to the specified collection.  

    Public WithEvents myOlItems As Outlook.Items 
     
    Public Sub Initialize_handler()  
     Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items  
    End Sub 
     
    Private Sub myOlItems_ItemAdd(ByVal Item As Object)  
     Dim myOlMItem As Outlook.MailItem  
     Dim myOlAtts As Outlook.Attachments 
     Set myOlMItem = myOlApp.CreateItem(olMailItem)  
     myOlMItem.Save  
     Set myOlAtts = myOlMItem.Attachments  
     ' Add new contact to attachments in mail message  
     myOlAtts.Add Item, olByValue  
     myOlMItem.To = "Sales Team"  
     myOlMItem.Subject = "New contact"  
     myOlMItem.Send  
    End Sub

    Be aware, the event is not fired when a large number of items are added to the folder at once (more than 16). This is a well known issue in Outlook (all versions)

    Also you may consider setting the SaveSentMessageFolder property of the MailItem class which allows to set a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent.

    Sub SetSentFolder()  
     Dim myItem As Outlook.MailITem  
     Dim myResponse As Outlook.MailITem  
     Dim mpfInbox As Outlook.Folder  
     Dim mpf As Outlook.Folder 
     
     Set mpfInbox = Application.Session.GetDefaultFolder(olFolderInbox)  
     Set mpf = mpfInbox.Folders.Add("SaveMyPersonalItems")  
     Set myItem = Application.ActiveInspector.CurrentItem  
     Set myResponse = myItem.Reply  
     myResponse.Display  
     myResponse.To = "Dan Wilson"  
     Set myResponse.SaveSentMessageFolder = mpf  
     myResponse.Send  
    End Sub
    So, the sent message will be added to the folder automatically.

    • Marked as answer by L.HlModerator Wednesday, July 29, 2015 9:24 AM
    Wednesday, July 15, 2015 7:37 AM
  • Hello,

    > I tried what you suggested but it didn't work. It created the folder, but didn't move the message from the sent items folder to the folder I specified.

    What code exactly did you try to run?

    > If there was a way to initialize the macro when a user pushes the sent button, that would be great.

    You may use the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks theSend button) or when the Send method for an Outlook item, such as MailItem, is used in a program.

    > Or if the macro can run every second that would be great too.

    You need to use a timer in that case. See Outlook VBA - Run a code every half an hour for more information.

    Finally, if you want to assign a VBA macro to a rule, the macro should look like the following one:

    Public Sub Test(mail as MailItem)
        '
    End Sub

    • Marked as answer by L.HlModerator Wednesday, July 29, 2015 9:24 AM
    Thursday, July 16, 2015 9:15 AM

All replies

  • Hello,

    You may handle the ItemAdd event of the Items class (comes from the folder where emails are located) which is fired when one or more items are added to the specified collection.  

    Public WithEvents myOlItems As Outlook.Items 
     
    Public Sub Initialize_handler()  
     Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items  
    End Sub 
     
    Private Sub myOlItems_ItemAdd(ByVal Item As Object)  
     Dim myOlMItem As Outlook.MailItem  
     Dim myOlAtts As Outlook.Attachments 
     Set myOlMItem = myOlApp.CreateItem(olMailItem)  
     myOlMItem.Save  
     Set myOlAtts = myOlMItem.Attachments  
     ' Add new contact to attachments in mail message  
     myOlAtts.Add Item, olByValue  
     myOlMItem.To = "Sales Team"  
     myOlMItem.Subject = "New contact"  
     myOlMItem.Send  
    End Sub

    Be aware, the event is not fired when a large number of items are added to the folder at once (more than 16). This is a well known issue in Outlook (all versions)

    Also you may consider setting the SaveSentMessageFolder property of the MailItem class which allows to set a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent.

    Sub SetSentFolder()  
     Dim myItem As Outlook.MailITem  
     Dim myResponse As Outlook.MailITem  
     Dim mpfInbox As Outlook.Folder  
     Dim mpf As Outlook.Folder 
     
     Set mpfInbox = Application.Session.GetDefaultFolder(olFolderInbox)  
     Set mpf = mpfInbox.Folders.Add("SaveMyPersonalItems")  
     Set myItem = Application.ActiveInspector.CurrentItem  
     Set myResponse = myItem.Reply  
     myResponse.Display  
     myResponse.To = "Dan Wilson"  
     Set myResponse.SaveSentMessageFolder = mpf  
     myResponse.Send  
    End Sub
    So, the sent message will be added to the folder automatically.

    • Marked as answer by L.HlModerator Wednesday, July 29, 2015 9:24 AM
    Wednesday, July 15, 2015 7:37 AM
  • Thanks for the reply,

    I tried what you suggested but it didn't work. It created the folder, but didn't move the message from the sent items folder to the folder I specified.

    I did try this code:

    Sub MoveItemsCUST()
     Dim myNameSpace As Outlook.NameSpace
     Dim myInbox As Outlook.Folder
     Dim myDestFolder As Outlook.Folder
     Dim myItems As Outlook.Items
     Dim myItem As Object
     
     Set myNameSpace = Application.GetNamespace("MAPI")
     Set myInbox = myNameSpace.GetDefaultFolder(olFolderSentMail)
     Set myItems = myInbox.Items
     Set myDestFolder = myInbox.Folders("STORED SENT ITEMS")
     Set myItem = myItems.Find("[SenderName] = 'SERVICE@DOMAIN.COM'")
     While TypeName(myItem) <> "Nothing"
     myItem.Move myDestFolder
     Set myItem = myItems.FindNext
     Wend
    End Sub
    

    And it works great, it goes through the sent items and finds any emails from "service@domain.com" and moves them into a folder called "Stored Sent Items", here is my issue - I have to actually run this macro to preform this task. This macro is actually for a client that can't have any user interaction of initializing the macro.

    I know I can have the macro launch at startup, but that means the macro will only run that one time, and this user sends emails all day from different accounts and they see it in their "Sent Items" folder.

    If there was a way to initialize the macro when a user pushes the sent button, that would be great. Or if the macro can run every second that would be great too.

    I've tried creating rules for moving the sent items, but it doesn't work, and for some reason when I try to create a rule, and choose the action "run a script" and try to choose my macro, the dialog box is empty and I can't choose any scripts. I was able to choose a script list time, but I'm not sure why I can't anymore.

    Sorry for the long reply, but always thank you for your help.

    Thursday, July 16, 2015 6:39 AM
  • Hello,

    > I tried what you suggested but it didn't work. It created the folder, but didn't move the message from the sent items folder to the folder I specified.

    What code exactly did you try to run?

    > If there was a way to initialize the macro when a user pushes the sent button, that would be great.

    You may use the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks theSend button) or when the Send method for an Outlook item, such as MailItem, is used in a program.

    > Or if the macro can run every second that would be great too.

    You need to use a timer in that case. See Outlook VBA - Run a code every half an hour for more information.

    Finally, if you want to assign a VBA macro to a rule, the macro should look like the following one:

    Public Sub Test(mail as MailItem)
        '
    End Sub

    • Marked as answer by L.HlModerator Wednesday, July 29, 2015 9:24 AM
    Thursday, July 16, 2015 9:15 AM