Using VBA to copy email to a different folder

mike 1 Reputation point
2021-10-28T21:31:44.05+00:00

I seem to have this almost working but it is sending 3 (and sometimes more) copies to the folder instead of just one. It is the last IF statement that I have changed., the one referencing: "PO Send to Kathy". All the others are for simply moving the email. But for this case I want to leave the email and just send a copy but the problem is, it is sending at multiple copies to that folder each time.

Here is the the code with the copy part added: Set objMail = objMail.Copy. Prior to that it was just with the move statement. Again, it does work, it just makes multiple copies. Any help would be appreciated. Thanks

'Occurs when changing item
Private Sub objInboxItemsOrder_ItemChange(ByVal Item As Object)
    Dim objMail As Outlook.MailItem
    Dim objTargetFolder As Outlook.Folder

    If TypeOf Item Is MailItem Then
       Set objMail = Item
       'Move mails based on color category
       If InStr(objMail.Categories, "Karen") > 0 Then
          Set objTargetFolder = Application.Session.Folders("orders@xxs.com").Folders("Karen")
          objMail.Move objTargetFolder
        ElseIf InStr(objMail.Categories, "Quote") > 0 Then
          Set objTargetFolder = Application.Session.Folders("orders@xxx.com").Folders("Quote")
          objMail.Move objTargetFolder
        ElseIf InStr(objMail.Categories, "PO Keep Here") > 0 Then
          Set objTargetFolder = Application.Session.Folders("orders@xxx.com").Folders("Purchase Order")
          objMail.Move objTargetFolder
        ElseIf InStr(objMail.Categories, "PO Send to Kathy") > 0 Then
          Set objTargetFolder = Application.Session.Folders("orders@xxx.com").Folders("PO Send to Kathy")
          Set objMail = objMail.Copy
          objMail.Move objTargetFolder
       End If
    End If
End Sub
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Tom van Stiphout 1,621 Reputation points MVP
    2021-10-28T23:44:49.82+00:00

    That would lead me to think the code is in the wrong event.
    The ItemAdd event of the Folder object comes to mind.

    0 comments No comments

  2. mike 1 Reputation point
    2021-10-29T03:24:01.913+00:00

    Thanks for the suggestion. I will try that and report back but it seems like if the item is the the email, we are changing it by assigning a category. That, I believe, is the trigger to the event?

    0 comments No comments