none
Snag - Remove Recipient on Reply vs. New Email RRS feed

  • Question

  • I was given some code by Fei Xue that works great on new messages, but does not work on replies and I cannot understand why. The goal is to check for some email addresses and remove them if they are in the To:, Cc:, or Bcc: boxes on ItemSend.

    It works great on a new email. But if you are replying or use reply-all, the script doesn't work. The segment where it asks if you are sending from the correct email works every time, but not the bit that removes the emails listed in the code.  That only works on a new email.

    Anyone have a clue as to why this may be?

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim aItem As mailitem
    Dim aRecipient As recipient
         
    On Error Resume Next
    
    Set aItem = Application.ActiveInspector.CurrentItem
        RemoveRecipientsBySMTPAddress aItem, "email1(at)adomain.com"
        RemoveRecipientsBySMTPAddress aItem, "email2(at)adomain.com"
        RemoveRecipientsBySMTPAddress aItem, "email3(at)adomain.com"
        RemoveRecipientsBySMTPAddress aItem, "email4(at)adomain.com"
    
    
    If InStr(LCase(Item.From), "accountemail(at)adomain.com") Then
          Prompt$ = "Is the From: address correct?"
           If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
             Cancel = True
           End If
      End If
         
    End Sub
    
    
    
    
    Sub RemoveRecipientsBySMTPAddress(mail As Outlook.mailitem, SMTPAddress As String)
        Dim recips As Outlook.Recipients
        Dim recip As Outlook.recipient
        Dim pa As Outlook.PropertyAccessor
        Const PR_SMTP_ADDRESS As String = _
            "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
        Set recips = mail.Recipients
        
        For i = recips.Count To 1 Step -1
             Set pa = recips(i).PropertyAccessor
              If pa.GetProperty(PR_SMTP_ADDRESS) = SMTPAddress Then
                recips(i).Delete
              End If
        Next i
     
    End Sub


    :: mjsjr

    Thursday, July 31, 2014 1:45 PM

Answers

  • Did you try to debug the code?

    Anyway, I have noticed the following line of code:

    Application.ActiveInspector.CurrentItem

    Is there any active inspector window?

    Note, the code is located in the ItemSend event handler. You need to use the Item parameter instead. 

    • Marked as answer by mjsjr Thursday, July 31, 2014 4:56 PM
    • Unmarked as answer by mjsjr Friday, August 1, 2014 5:25 PM
    • Marked as answer by mjsjr Friday, August 1, 2014 5:25 PM
    Thursday, July 31, 2014 2:13 PM
  • What Eugene was suggesting was changing this line:

    Set aItem = Application.ActiveInspector.CurrentItem

    to this:

    Set aItem = Item

    or just using 'Item' as it's passed to you in the event handler wherever you'd use aItem.


    Ken Slovak MVP - Outlook

    • Proposed as answer by George HuaModerator Friday, August 1, 2014 7:33 AM
    • Marked as answer by mjsjr Friday, August 1, 2014 5:24 PM
    Thursday, July 31, 2014 3:11 PM
    Moderator
  • Thank you, Eugene. Changing the line...

    from:  Set aItem = Application.ActiveInspector.CurrentItem

    to: Set aItem = Item

    Made all the difference.  I still don't understand why using the first model works with new emails only but I am delighted that this solution works!

    Thanks again!


    :: mjsjr

    • Marked as answer by mjsjr Friday, August 1, 2014 5:25 PM
    Thursday, July 31, 2014 4:58 PM

All replies

  • It should also work with replies. What is happening? Are you getting any errors?

    Have you tried to step the code in debug mode and seeing what happens in the RemoveRecipientsBySMTPAddress() procedure? Put a breakpoint there and when the breakpoint is hit use F8 to step the code and examine the variables in each step to see what's happening.


    Ken Slovak MVP - Outlook

    Thursday, July 31, 2014 1:54 PM
    Moderator
  • Did you try to debug the code?

    Anyway, I have noticed the following line of code:

    Application.ActiveInspector.CurrentItem

    Is there any active inspector window?

    Note, the code is located in the ItemSend event handler. You need to use the Item parameter instead. 

    • Marked as answer by mjsjr Thursday, July 31, 2014 4:56 PM
    • Unmarked as answer by mjsjr Friday, August 1, 2014 5:25 PM
    • Marked as answer by mjsjr Friday, August 1, 2014 5:25 PM
    Thursday, July 31, 2014 2:13 PM
  • There aren't any errors. On new email, it just works when you press send. If it's a reply, it just ignores it completely sends away. I will try the debug mode and see what happens here.

    :: mjsjr

    Thursday, July 31, 2014 3:02 PM
  • If I use the Item parameter instead of the ItemSend handler, what will be the trigger to run the code and remove the emails I would like deleted? Seemed that ItemSend was a good time to delete them.  Do you have another thought? Perhaps I don't understand.

    :: mjsjr

    Thursday, July 31, 2014 3:04 PM
  • What Eugene was suggesting was changing this line:

    Set aItem = Application.ActiveInspector.CurrentItem

    to this:

    Set aItem = Item

    or just using 'Item' as it's passed to you in the event handler wherever you'd use aItem.


    Ken Slovak MVP - Outlook

    • Proposed as answer by George HuaModerator Friday, August 1, 2014 7:33 AM
    • Marked as answer by mjsjr Friday, August 1, 2014 5:24 PM
    Thursday, July 31, 2014 3:11 PM
    Moderator
  • There is no need to ask for the active inspector instance. The current item is delivered via the first parameter to the ItemSend event handler.

    Did you have a chance to change the line of code? Did it help?

    Thursday, July 31, 2014 3:57 PM
  • Thank you, Eugene. Changing the line...

    from:  Set aItem = Application.ActiveInspector.CurrentItem

    to: Set aItem = Item

    Made all the difference.  I still don't understand why using the first model works with new emails only but I am delighted that this solution works!

    Thanks again!


    :: mjsjr

    • Marked as answer by mjsjr Friday, August 1, 2014 5:25 PM
    Thursday, July 31, 2014 4:58 PM
  • Thanks, Ken.  That was it.  Works great now!

    :: mjsjr

    Thursday, July 31, 2014 5:00 PM