none
Outlook 2010- sort and forward email based on text - need help with VBA code RRS feed

  • Question

  • Hi everyone

    Wondering if one of the expert in programming could help me. 

    I receive emails with a field indicating country and product name in the text. I need to change the subject line based on product name (i have the choice between product A and B), forward each email to a given region (group of country, based on the country in the email) and move to a regional folder for this product. 

    So basically, if i receive an email that indicate the country France and product A, i need to change the subject line of the email to say 'product A', send the modified email to an email address for Europe, and file to a folder "Product A Europe" (folder already created)

    -> is there a way to determine region based on country using maybe a master table? And if so, how can i do that?

    -> would anyone have code available for what I'm trying to describe? 

    We've tried with rules based on text and the following script 

    Sub test(Item As Outlook.MailItem)
        Item.Subject = "Test"
    Item.Save

    End Sub

    but we're struggling...

    Many thanks in advance for your help!

    best regards

    Nathalie 


    • Edited by nathalie79 Tuesday, June 25, 2013 12:03 PM
    Tuesday, June 25, 2013 12:02 PM

Answers

  • It looks like you have a rule defined to run a script.  However, your script is trying to modify the received email.  Don't you want to reply to it with a new email?  You can then pass Item.Subject to the new email's .Subject property and send it.  However, you'd have to manually parse the Item.Body property to get the product and region text. 

    To move the email to another folder, call Item.Move and pass a Folder object representing the folder you want to move it to.  How you get that Folder object depends on whether you know the EntryID of the folder (for use with Namespace.GetFolderFromID), or the location of the folder (by "walking" the Folders collection from one or more parent folders in your folder hierarchy to get a Folder object for a subfolder by name with the Folders.Item method.


    Eric Legault (MVP: Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Tuesday, June 25, 2013 2:43 PM
    Moderator
  • Yes, nothing changes from what I suggested - other than you use Item.Forward instead of Item.Reply to get a new MailItem object that you work with.  If you have any questions, let me know.  Otherwise, there's plenty of learning material here:

    OutlookCode.com - Developer Learning Center for Microsoft Outlook
    http://www.outlookcode.com/default.aspx

    Concepts (Outlook 2013 developer reference)
    http://msdn.microsoft.com/en-us/library/ff868025.aspx


    Eric Legault (MVP: Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Tuesday, July 2, 2013 3:21 PM
    Moderator

All replies

  • It looks like you have a rule defined to run a script.  However, your script is trying to modify the received email.  Don't you want to reply to it with a new email?  You can then pass Item.Subject to the new email's .Subject property and send it.  However, you'd have to manually parse the Item.Body property to get the product and region text. 

    To move the email to another folder, call Item.Move and pass a Folder object representing the folder you want to move it to.  How you get that Folder object depends on whether you know the EntryID of the folder (for use with Namespace.GetFolderFromID), or the location of the folder (by "walking" the Folders collection from one or more parent folders in your folder hierarchy to get a Folder object for a subfolder by name with the Folders.Item method.


    Eric Legault (MVP: Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Tuesday, June 25, 2013 2:43 PM
    Moderator
  • Hi Eric,

    Sorry for replying late - for some reason, i didn't see your reply before :-(

    One question: i shouldn't reply to the email but i need to forward. I guess this would work as well and I could still use the item.subject... that you suggested, correct?

    For the other points, I need to try... I've never done this before but that's the way to learn i guess :)

    If you don't mind, i'll keep replying to this post if I struggle too much. And will put the final code.

    Thanks!

    Nathalie

    Tuesday, July 2, 2013 9:21 AM
  • Yes, nothing changes from what I suggested - other than you use Item.Forward instead of Item.Reply to get a new MailItem object that you work with.  If you have any questions, let me know.  Otherwise, there's plenty of learning material here:

    OutlookCode.com - Developer Learning Center for Microsoft Outlook
    http://www.outlookcode.com/default.aspx

    Concepts (Outlook 2013 developer reference)
    http://msdn.microsoft.com/en-us/library/ff868025.aspx


    Eric Legault (MVP: Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Tuesday, July 2, 2013 3:21 PM
    Moderator