0

I'm trying to "reply all", add text to the subject, add a recipient, and remove a recipient.

Sub Reply_All()
    Dim olReply As mailitem
    Dim strSubject As String
    For Each olItem In Application.ActiveExplorer.Selection
        Set olReply = olItem.ReplyAll
        Set olRecip = olReply.Recipients.Add("EmailAddressGoesHere")
        Set olRecip = olReply.Recipients.Remove("EmailAddressGoesHere")
        strSubject = olReply.Subject
        olReply.Subject = "(Added Subject Line Info - ) " & strSubject
        olReply.Display
    Next
End Sub

Everything works when I comment out the Recipients.Remove line.

I noticed that

Set olRecip = olReply.Recipients.Add("EmailAddressGoesHere")

has "Add Name As String"

While

Set olRecip = olReply.Recipients.Remove("EmailAddressGoesHere")

has "Remove Index As Long" as the yellow text that comes up when you type it into the script.

Community
  • 1
  • 1
Pyrometheous
  • 65
  • 2
  • 14
  • Looking at the [doc](https://learn.microsoft.com/en-us/office/vba/api/outlook.recipients.remove), Remove does seems to only accept a Long index, not a String. – Vincent G Oct 17 '18 at 15:31
  • Yeah, I found that too, I'm just not familiar with how to use an index in an outlook macro. – Pyrometheous Oct 17 '18 at 15:36
  • You can use a `For` loop to locate and remove the recipient. – Vincent G Oct 17 '18 at 15:37
  • There is also a [Recipient.Delete](https://learn.microsoft.com/en-us/office/vba/api/outlook.recipient.delete) Method, not sure if it could work. – Vincent G Oct 17 '18 at 15:40
  • I can't figure out how to use those methods. It's driving me crazy that I can't figure this out. Microsoft's documentation doesn't outright say how to use it, it just says that they are methods for "Recipient" or "Recipients", but doesn't show an example or anything. I'm also having trouble figuring out "Long" and "Index". Do you have any articles you could suggest on a place to start reading up on it? – Pyrometheous Oct 17 '18 at 18:51

4 Answers4

0

Loop through the recipients using a "for" loop from Count down to 1, check the Recipient.Address property. If it matches the value you are after, call Recipients.Remove passing the current loop index.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I'll mess around with using a "for" loop in about an hour or so, I've got a couple meetings scheduled coming up in a few minutes. I don't think I've done loops in an Outlook macro yet, so this should be fun to learn. Thank you for the suggestion. I'll update you once I've had the opportunity to try it. – Pyrometheous Oct 17 '18 at 15:52
  • I'm having a really hard time figuring this out, earlier I found something that explained the index and creating a list (I think), but I accidentally closed a bunch of tabs and lost the page. Can you direct me to an article that explains "Index" and "long"? – Pyrometheous Oct 17 '18 at 18:27
0

As Dmitry mentioned, you could refer to the below code:

    Sub Reply_All()
    Dim olReply As MailItem
    Dim strSubject As String
    For Each olItem In Application.ActiveExplorer.Selection
    Set olReply = olItem.ReplyAll
    For Each Address In EmailAddressGoesHere
    olReply.Recipients.Add (Address)
    Next
    For Each Rec In olReply.Recipients
        Rec.Delete
    Next
    strSubject = olReply.Subject
        olReply.Subject = "(Added Subject Line Info - ) " & strSubject
        olReply.Display
    Next
    End Sub

For more information, please refer to this link:

remove recipient from mail.recipient collection

Alina Li
  • 884
  • 1
  • 6
  • 5
  • Not a problem if only one item is processed but it is safer to count down when reducing a collection with delete or move. https://stackoverflow.com/questions/10725068/for-each-loop-some-items-get-skipped-when-looping-through-outlook-mailbox-to-de – niton Oct 18 '18 at 11:45
  • I've been messing around with this a bit and I'm getting a Compile Error `Expected: variable`. Messing around more with it I thought I had it working correctly, but it ended up removing all recipients and adding the extra recipient I needed. I'm pretty sure my problem is here `For Each Address In EmailAddressGoesHere` I've tried different ways of putting in the email address, but get errors every time. – Pyrometheous Oct 18 '18 at 14:49
  • EmailAddressGoesHere must be a variable with a type that's a collection type or Object. Refers to the collection over which the statements are to be repeated. Please refer to this link: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-each-next-statement – Alina Li Oct 19 '18 at 01:44
0
Option Explicit
' Consider Option Explicit mandatory
' Tools | Options | Editor tab | Require Variable Declaration

Sub Reply_All_RemoveSingleOrMultipleCopiesAddress()

    Dim olItem As Object
    Dim olReply As MailItem

    Dim i As Long

    For Each olItem In ActiveExplorer.Selection

        If olItem.Class = olMail Then

            Set olReply = olItem.ReplyAll

            'olReply.Display

            ' If the address could occur once or multiple times,
            '  start at the end and work backwards
            For i = olReply.Recipients.count To 1 Step -1

                'Debug.Print olReply.Recipients(i).Address

                ' "EmailAddressToBeRemoved" with the quotes as shown
                If LCase(olReply.Recipients(i).Address) = LCase("EmailAddressToBeRemoved") Then
                    olReply.Recipients.remove (i)
                End If

            Next

            olReply.Display

        End If

    Next

End Sub


Sub Reply_All_RemoveSingleAddressReliably()

    Dim olItem As Object
    Dim olReply As MailItem
    Dim recip As recipient

    For Each olItem In ActiveExplorer.Selection

        If olItem.Class = olMail Then

            Set olReply = olItem.ReplyAll

            'olReply.Display

            ' If the address can appear once only,
            '  otherwise use a downward counting loop
            For Each recip In olReply.Recipients

                'Debug.Print recip.Address

                ' "EmailAddressToBeRemoved" with the quotes as shown 
                If LCase(recip.Address) = LCase("EmailAddressToBeRemoved") Then

                    ' Delete not remove
                    recip.Delete

                    ' No need to continue if only one instance of address can occur,
                    '  otherwise you would unreliably delete anyway.
                    ' The address immediately after a deleted address is skipped
                    '  as it moves into the old position of the deleted address.
                    Exit For

                End If

            Next

            olReply.Display

        End If

    Next

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
0

To whom it may concern.

You can easily try a combination of the solutions offered for a quick result:

Set myRecipients = olReply.Recipients

Dim y As Long
y = myRecipients.Count

Do Until y = 0
    If myRecipients(y) = "to be removed" Then
    myRecipients(y).Delete
    End If
    y = y - 1
Loop