none
Outlook Rule Fires Macro before Moving Mail to the required Folder RRS feed

  • Question

  • I initially had this code running to save attachments from emails that come in. My code would loop through the entire folder and for any attachments that were there, it would save the attachment and remove it from the email. I had a rule in place to make the macro fire whenever the mail I wanted came in. However the attachment would never save down when the mail came. There were no errors, and debugging manually worked just fine. In addition, running the rule immediately afterwards by clicking the 'Run rules now' button would work just fine too. So after trying out a million different ways to save the attachments, I started getting really annoyed and set up a test to see what the hell outlook was doing. So here is the problem.

    This is the outlook rule I have set up:

    Apply this rule after the message arrives from xxxxx move it to the Macrotest folder and run Project1.ThisOutlookSession.sayhi

    I discovered though that what outlook does is fire the script Before it transfers the mail into the folder my macro looks in. Hence, it never finds the new file. Obviously then when its done the mail comes into the folder so when I run it manually it works just fine. So how can I fix the order that this rule comes in?

     Public Sub sayhi(item As Outlook.MailItem)
    
     Dim objNS As Outlook.NameSpace
     Dim olfolder As Outlook.MAPIFolder
     Dim msg As Outlook.MailItem
    
     Set objNS = Application.GetNamespace("MAPI")
     Set olfolder = objNS.GetDefaultFolder(olFolderInbox)
     Set olsubfolder = olfolder.Folders("Macrotest")
     Set oapp = CreateObject("Shell.Application")
    
    
          For Each myitem In olsubfolder.Items
    
                MsgBox "hellothere"
    
                myitem.UnRead = False
    
          Next
    
    
    End Sub
    Saturday, March 2, 2013 12:15 AM

Answers

  • I'd suggest that your best approach would be to have the rule do the move and then have a Folder.Items.ItemAdd() event handler set up for when an item is added to the destination folder. That handler can then remove the attachments.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "MorganIndust" <=?utf-8?B?TW9yZ2FuSW5kdXN0?=> wrote in message news:fa05a7d6-9915-4fbd-a1e1-83f10b22e1e2...

    I initially had this code running to save attachments from emails that come in. My code would loop through the entire folder and for any attachments that were there, it would save the attachment and remove it from the email. I had a rule in place to make the macro fire whenever the mail I wanted came in. However the attachment would never save down when the mail came. There were no errors, and debugging manually worked just fine. In addition, running the rule immediately afterwards by clicking the 'Run rules now' button would work just fine too. So after trying out a million different ways to save the attachments, I started getting really annoyed and set up a test to see what the hell outlook was doing. So here is the problem.

    This is the outlook rule I have set up:

    Apply this rule after the message arrives from xxxxx move it to the Macrotest folder and run Project1.ThisOutlookSession.sayhi

    I discovered though that what outlook does is fire the script Before it transfers the mail into the folder my macro looks in. Hence, it never finds the new file. Obviously then when its done the mail comes into the folder so when I run it manually it works just fine. So how can I fix the order that this rule comes in?

     Public Sub sayhi(item As Outlook.MailItem)
    
     Dim objNS As Outlook.NameSpace
     Dim olfolder As Outlook.MAPIFolder
     Dim msg As Outlook.MailItem
    
     Set objNS = Application.GetNamespace("MAPI")
     Set olfolder = objNS.GetDefaultFolder(olFolderInbox)
     Set olsubfolder = olfolder.Folders("Macrotest")
     Set oapp = CreateObject("Shell.Application")
    
    
          For Each myitem In olsubfolder.Items
    
                MsgBox "hellothere"
    
                myitem.UnRead = False
    
          Next
    
    
    End Sub

    Ken Slovak MVP - Outlook
    Monday, March 4, 2013 6:15 PM
    Moderator

All replies

  • Hi MorganIndust,

    It's based on my experience that your need creat a new model to implement your sayhi method. please try.

    Have a good day,

    Tom

    Monday, March 4, 2013 2:26 AM
    Moderator
  • I'd suggest that your best approach would be to have the rule do the move and then have a Folder.Items.ItemAdd() event handler set up for when an item is added to the destination folder. That handler can then remove the attachments.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "MorganIndust" <=?utf-8?B?TW9yZ2FuSW5kdXN0?=> wrote in message news:fa05a7d6-9915-4fbd-a1e1-83f10b22e1e2...

    I initially had this code running to save attachments from emails that come in. My code would loop through the entire folder and for any attachments that were there, it would save the attachment and remove it from the email. I had a rule in place to make the macro fire whenever the mail I wanted came in. However the attachment would never save down when the mail came. There were no errors, and debugging manually worked just fine. In addition, running the rule immediately afterwards by clicking the 'Run rules now' button would work just fine too. So after trying out a million different ways to save the attachments, I started getting really annoyed and set up a test to see what the hell outlook was doing. So here is the problem.

    This is the outlook rule I have set up:

    Apply this rule after the message arrives from xxxxx move it to the Macrotest folder and run Project1.ThisOutlookSession.sayhi

    I discovered though that what outlook does is fire the script Before it transfers the mail into the folder my macro looks in. Hence, it never finds the new file. Obviously then when its done the mail comes into the folder so when I run it manually it works just fine. So how can I fix the order that this rule comes in?

     Public Sub sayhi(item As Outlook.MailItem)
    
     Dim objNS As Outlook.NameSpace
     Dim olfolder As Outlook.MAPIFolder
     Dim msg As Outlook.MailItem
    
     Set objNS = Application.GetNamespace("MAPI")
     Set olfolder = objNS.GetDefaultFolder(olFolderInbox)
     Set olsubfolder = olfolder.Folders("Macrotest")
     Set oapp = CreateObject("Shell.Application")
    
    
          For Each myitem In olsubfolder.Items
    
                MsgBox "hellothere"
    
                myitem.UnRead = False
    
          Next
    
    
    End Sub

    Ken Slovak MVP - Outlook
    Monday, March 4, 2013 6:15 PM
    Moderator